11

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?

Hemang
  • 26,840
  • 19
  • 119
  • 186
debuggenius
  • 449
  • 5
  • 15

4 Answers4

11

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.

Hemang
  • 26,840
  • 19
  • 119
  • 186
5

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#>
}
Wiingaard
  • 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
3

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.

Miguel Isla
  • 1,379
  • 14
  • 25
Hemang
  • 26,840
  • 19
  • 119
  • 186
2

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

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45