objective-clocationmkmapviewcllocationmanagershowuserlocation

How to know if MKMapView is allowed to use current location?


I am showing the user location on a mapView with:

self.mapView.showsUserLocation = YES;

The user gets prompted the AlertView where he can choose whether to allow to use the current location or not. If he presses yes everything is ok and I do not worry about it.

But if he presses NO I would like to zoom to a specific region.

So how do I know whether the MKMapView is allowed to use the current location?

I found the solution where I would create my own CLLocationManager and its delegate to see if it returns an denied error. But this does not quite feel right, why introduce a new CLLocationManger if I do not need it.

Isn't there an other way?


Solution

  • You don't need a delegate. Just use the CLLocationManager class method authorizationStatus:

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        // allowed
    } else {
        // not allowed
    }
    

    The possible values are:

    typedef enum {
       kCLAuthorizationStatusNotDetermined = 0,
       kCLAuthorizationStatusRestricted,
       kCLAuthorizationStatusDenied,
       kCLAuthorizationStatusAuthorized
    } CLAuthorizationStatus;