CLLocationManager AuthorizationStatus callback? CLLocationManager AuthorizationStatus callback? ios ios

CLLocationManager AuthorizationStatus callback?


You can use the locationManager:didChangeAuthorizationStatus: CLLocationManagerDelegate method as a "callback" of sorts.

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {    if (status == kCLAuthorizationStatusDenied) {        // The user denied authorization    }    else if (status == kCLAuthorizationStatusAuthorized) {        // The user accepted authorization    }}

And in Swift (update suggested by user Michael Marvick, but rejected for some reason...):

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {    if (status == CLAuthorizationStatus.denied) {        // The user denied authorization    } else if (status == CLAuthorizationStatus.authorizedAlways) {        // The user accepted authorization    } }


Swift 3

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {    if (status == CLAuthorizationStatus.denied) {        // The user denied authorization    } else if (status == CLAuthorizationStatus.authorizedAlways) {        // The user accepted authorization    } }


When the authorisation status for location changes, the delegate method didChangeAuthorizationStatus: will be called.

When you call requestWhenInUseAuthorization the first time after your app is installed the delegate method will be called with status kCLAuthorizationStatusNotDetermined (0).

If the user declines location services access then the delegate method will be called again with status kCLAuthorizationStatusDenied (2).

If the user approves location services access then the delegate method will be called again with status kCLAuthorizationStatusAuthorizedAlways (3) or kCLAuthorizationStatusAuthorizedWhenInUse (4) depending on the permission that was requested.

On subsequent executions of your app the delegate method will receive status kCLAuthorizationStatusDenied or kCLAuthorizationStatusAuthorizedAlways/kCLAuthorizationStatusAuthorizedWhenInUse after calling requestWhenInUseAuthorization based on the current state of location services permission for the app in the device settings.