【发布时间】:2015-02-10 18:14:02
【问题描述】:
如果用户已明确拒绝此应用程序的授权,或者在设置中禁用了定位服务,它将返回拒绝状态。我怎么知道它的确切原因?
【问题讨论】:
标签: ios objective-c swift cllocationmanager
如果用户已明确拒绝此应用程序的授权,或者在设置中禁用了定位服务,它将返回拒绝状态。我怎么知道它的确切原因?
【问题讨论】:
标签: ios objective-c swift cllocationmanager
既然'authorizationStatus()' was deprecated in iOS 14.0你应该直接使用实例。
authorizationStatus 现在是CLLocationManager 的属性。
您可以使用以下示例:
import CoreLocation
class LocationManager {
private let locationManager = CLLocationManager()
var locationStatus: CLAuthorizationStatus {
locationManager.authorizationStatus
}
}
也看看这个问题:AuthorizationStatus for CLLocationManager is deprecated on iOS 14
【讨论】:
斯威夫特:
我已经制作了这两个函数来检查每个案例
如果用户明确拒绝对您的应用授权,您可以这样检查,
class func isLocationDisableForMyAppOnly() -> Bool {
if CLLocationManager.locationServicesEnabled() && CLLocationManager.authorizationStatus() == .denied {
return true
}
return false
}
如果在“设置”中禁用了定位服务,
class func userLocationAvailable() -> Bool {
return CLLocationManager.locationServicesEnabled()
}
我就是这样用的,
if UserLocation.userLocationAvailable() {
//.... Get location.
} else {
if UserLocation.isLocationDisableForMyAppOnly() {
//... Location not available. Denied accessing location.
} else {
//... Location not available. Enable location services from settings.
}
}
附注UserLocation 是一个获取用户位置的自定义类。
【讨论】:
斯威夫特
用途:
CLLocationManager.authorizationStatus()
获取授权状态。这是一个CLAuthorizationStatus,你可以像这样打开不同的状态:
let status = CLLocationManager.authorizationStatus()
switch status {
case .authorizedAlways:
<#code#>
case .authorizedWhenInUse:
<#code#>
case .denied:
<#code#>
case .notDetermined:
<#code#>
case .restricted:
<#code#>
}
【讨论】:
我已经制作了这两个函数来检查每个案例
如果用户明确拒绝对您的应用授权,您可以这样检查,
+ (BOOL) isLocationDisableForMyAppOnly
{
if([CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
return YES;
}
return NO;
}
如果在“设置”中禁用了定位服务,
+ (BOOL) userLocationAvailable {
return [CLLocationManager locationServicesEnabled];
}
我就是这样用的,
if([UserLocation userLocationAvailable]) {
//.... Get location.
}
else
{
if([UserLocation isLocationDisableForMyAppOnly]) {
//... Location not available. Denied accessing location.
}
else{
//... Location not available. Enable location services from settings.
}
}
附注UserLocation 是一个获取用户位置的自定义类。
【讨论】: