12

IOS4 最近引入了为特定应用程序启用/禁用位置服务的可能性。

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

首先,我尝试过:

if ([CLLocationManager locationServicesEnabled])
    {
       ....
    } 

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

其次我尝试使用

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   ...
} 

它可以工作,但在禁用服务应用程序设置的情况下以及在其他情况下(例如,如果某项措施因某种原因失败)都会被调用。

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

我怎样才能做到这一点?

谢谢你的支持

4

3 回答 3

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

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

于 2010-09-30T13:48:40.517 回答
10

我更喜欢使用

-(BOOL)locationAuthorized {
  return ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized);
}

在 locationServicesEnabled 属性上,因为它指的是电话级别,而不是您的应用程序。

于 2013-01-16T01:44:30.157 回答
1

根据CLLocationManager 上的 Apple 文档,有一个名为的属性+ (BOOL)locationServicesEnabled应该可以做到这一点。

于 2012-05-09T14:04:43.450 回答