简体   繁体   中英

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?

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.
    }
}

PS UserLocation is a custom class to get user location.

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.
    }
}

PS UserLocation is a custom class to get user location.

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#>
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM