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?
4 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.
 
    
    - 26,840
- 19
- 119
- 186
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#>
}
 
    
    - 4,150
- 4
- 35
- 67
- 
                    how can I tell what the user selected? AKA how can I know _when_ the user has selected something from the "allow...to access your location" modal? – Aug 26 '18 at 23:12
Swift:
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,
class func isLocationDisableForMyAppOnly() -> Bool {
    if CLLocationManager.locationServicesEnabled() && CLLocationManager.authorizationStatus() == .denied {
        return true
    }
    return false
}
If location services are disabled in Settings,
class func userLocationAvailable() -> Bool {
    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.
 
    
    - 1,379
- 14
- 25
 
    
    - 26,840
- 19
- 119
- 186
Swift 5.0 iOS 14.0
Since 'authorizationStatus()' was deprecated in iOS 14.0 you should use the instance directly.
authorizationStatus is now a property of CLLocationManager.
You can use the following example:
import CoreLocation
class LocationManager {
    private let locationManager = CLLocationManager()
    
    var locationStatus: CLAuthorizationStatus {
        locationManager.authorizationStatus
    }
}
Have also a look at this question: AuthorizationStatus for CLLocationManager is deprecated on iOS 14
 
    
    - 3,513
- 1
- 30
- 45
