Print location shared when user tap 'locate me' button

Hello,

I've implemented a button which locates user on map. I would also to inform user, when location sharing for this app is set to never, that user needs to turn it on in settings. Here are those functions:

    func checkLocationServices() {

      if CLLocationManager.locationServicesEnabled() {

        checkLocationAuthorization()

      } else {

        // Show alert letting the user know they have to turn this on.

      }

    }
    func checkLocationAuthorization() {

      let manager = CLLocationManager()

      switch manager.authorizationStatus {

      case .authorizedWhenInUse:

        mapView.showsUserLocation = true

       case .denied: // Show alert telling users how to turn on permissions

       break

      case .notDetermined:

        locationManager.requestWhenInUseAuthorization()

        mapView.showsUserLocation = true

      case .restricted: // Show an alert letting them know what’s up

       break

      case .authorizedAlways:

       break

      @unknown default:

          break

      }

}

    @IBAction func myLocationButtonTapped(_ sender: Any) {

        mapView.showsUserLocation = true

        mapView.setUserTrackingMode(.follow, animated: true)



    }

    

What is it you don't know how to do ?

To display an alert:

          let alert = UIAlertController(title: "Notice", message: "Check location", preferredStyle: .alert)
          
          alert.addAction(UIAlertAction(
               title: "OK",
               style: UIAlertAction.Style.default,
               handler: { _ in                                  
                    DispatchQueue.main.async {  // to update UI
                        // Here I segue to another VC
                         if let passDataVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NoticeViewID") as? NoticeScrollViewController {
                              self.present(passDataVC, animated: true, completion: nil)
                         }
                    }
               }))
          self.present(alert, animated: true, completion: { print("some message") } )

@OOPer I mean permission to use user location. When user click 'never' while the app is starting, I would like to show an alert after clicking the myLocationButtonTapped that loacation services for this app are off and user need to take an action in settings o turn it on.

Accepted Answer

What about changing like this:

    func checkLocationAuthorization() {

        let manager = CLLocationManager()
         switch manager.authorizationStatus {
         case .authorizedWhenInUse: print("authorizedWhenInUse")

          case .denied: print("denied")                    // Show alert telling users how to turn on permissions
             let alertController = UIAlertController(title: "Localisation denied", message: "Change privacy settings for the app", preferredStyle: .alert)
             let okAction = UIAlertAction(title: "OK", style: .cancel, handler: {action in})
             alertController.addAction(okAction)
             present(alertController, animated: true, completion: nil)

         case .notDetermined: print("notDetermined")

         case .restricted: print("restricted")// Show an alert letting them know what’s up

         case .authorizedAlways: print("authorizedAlways")

         @unknown default:   break

         }
}

Is it normal that when user choose allow once, current location disappears in couple of seconds when app is in background? 

I cannot tell for sure, but once means that when it's done, it is no more authorised. Backgrounding may fall in this case. What you could do is instruct your user via the message in info.plist

For the rest, if it works, don't forget to close the thread. And ask another question if you have other points to solve.

Print location shared when user tap 'locate me' button
 
 
Q