如何检测我的应用程序的位置服务禁用?

Mas*_*imo 12 iphone core-location cllocationmanager

IOS4最近推出了为特定应用启用/禁用位置服务的可能性.

我需要检测是否为MY应用程序启用/禁用了此设置.

首先我尝试过:

if ([CLLocationManager locationServicesEnabled])
    {
       ....
    } 
Run Code Online (Sandbox Code Playgroud)

但是,这指的是全局位置服务,而不是特定的应用程序设置.

其次我试过用

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   ...
} 
Run Code Online (Sandbox Code Playgroud)

它可以工作但是在服务应用程序设置被禁用的情况下被调用,在其他情况下,例如,如果某个度量由于某些原因而失败.

我需要一个代码来检测是否允许我的应用程序使用位置服务.

我怎样才能实现这一目标?

感谢您的支持

Kri*_*kel 24

locationManager的文档:didFailWithError:

如果用户拒绝您的应用程序使用位置服务,则此方法会报告kCLErrorDenied错误.收到此类错误后,您应该停止位置服务.

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if ([[error domain] isEqualToString: kCLErrorDomain] && [error code] == kCLErrorDenied) {
        // The user denied your app access to location information.
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到其他错误代码.


Joe*_*Joe 10

我更喜欢使用

-(BOOL)locationAuthorized {
  return ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized);
}
Run Code Online (Sandbox Code Playgroud)

在locationServicesEnabled属性上,因为它指的是手机级别,而不是您的应用程序.