如何获得位置服务的确切授权状态?

时间:2022-09-06 20:55:44

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?

如果用户明确拒绝此应用程序的授权,或者在“设置”中禁用了位置服务,则它将返回“拒绝”状态。我怎么知道它的确切原因?

3 个解决方案

#1


4  

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.

附: UserLocation是一个获取用户位置的自定义类。

#2


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.

附: UserLocation是一个获取用户位置的自定义类。

#3


1  

Swift

Use:

CLLocationManager.authorizationStatus()

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

获得授权状态。这是一个CLAuthorizationStatus,你可以像这样打开不同的状态:

let status = CLLocationManager.authorizationStatus()
switch status {
    case .authorizedAlways:
        <#code#>
    case .authorizedWhenInUse:
        <#code#>
    case .denied:
        <#code#>
    case .notDetermined:
        <#code#>
    case .restricted:
        <#code#>
}

#1


4  

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.

附: UserLocation是一个获取用户位置的自定义类。

#2


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.

附: UserLocation是一个获取用户位置的自定义类。

#3


1  

Swift

Use:

CLLocationManager.authorizationStatus()

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

获得授权状态。这是一个CLAuthorizationStatus,你可以像这样打开不同的状态:

let status = CLLocationManager.authorizationStatus()
switch status {
    case .authorizedAlways:
        <#code#>
    case .authorizedWhenInUse:
        <#code#>
    case .denied:
        <#code#>
    case .notDetermined:
        <#code#>
    case .restricted:
        <#code#>
}