Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the exact authorization status of location service?

If user has explicitly denied authorization for this application, or location services are disabled in Settings, it will return Denied status. How can I know the exact reason of it?

like image 740
debuggenius Avatar asked Dec 12 '14 04:12

debuggenius


2 Answers

I've made this two function to check for each case


If user explicitly denied authorization for your app only you can check it like this,

+ (BOOL) isLocationDisableForMyAppOnly
{
    if([CLLocationManager locationServicesEnabled] &&
       [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
    {
        return YES;
    }

    return NO;
}

If location services are disabled in Settings,

+ (BOOL) userLocationAvailable {
    return [CLLocationManager locationServicesEnabled];
}

And I'm using it like this,

if([UserLocation userLocationAvailable]) {
    //.... Get location.
}
else
{
    if([UserLocation isLocationDisableForMyAppOnly]) {
        //... Location not available. Denied accessing location.
    }
    else{
        //... Location not available. Enable location services from settings.
    }
}

P.S. UserLocation is a custom class to get user location.

like image 196
Hemang Avatar answered Sep 18 '22 14:09

Hemang


Swift

Use:

CLLocationManager.authorizationStatus()

To get the authorization status. It's a CLAuthorizationStatus, you can switch on the different status' like this:

let status = CLLocationManager.authorizationStatus()
switch status {
    case .authorizedAlways:
        <#code#>
    case .authorizedWhenInUse:
        <#code#>
    case .denied:
        <#code#>
    case .notDetermined:
        <#code#>
    case .restricted:
        <#code#>
}
like image 38
Wiingaard Avatar answered Sep 16 '22 14:09

Wiingaard