位置服务行为怪异
Posted
技术标签:
【中文标题】位置服务行为怪异【英文标题】:Location services behaving weird 【发布时间】:2014-03-28 05:35:23 【问题描述】:我想检测位置服务是否启用,并基于我想启用或禁用我的应用程序中的按钮。为此我写了
if([CLLocationManager locationServicesEnabled])
// Enable button
else
// Disbale button
但是,我发现这种方法有一个奇怪的行为。在设置中,
1)如果我关闭位置服务,对于所有应用程序,上述方法返回 NO。(如预期)
2) 如果我打开 locationServices,但对我的特定应用程序关闭,则返回 YES。
这是正确的行为吗?如果是,那么是否有任何其他方法可以确定在应用程序级别是否启用或禁用了位置服务。任何想法。
【问题讨论】:
【参考方案1】:当您使用locationServicesEnabled
方法来查找是否为您的应用启用了位置服务时,我想明确的是locationServicesEnabled
检测是否为设备启用了位置服务。它不检查特定应用程序。
来自Apple Docs返回一个布尔值,表示设备上是否启用了定位服务
您可以使用locationManager:didFailWithError:
来检测特定应用的定位服务
来自Apple documentation
如果用户拒绝你的应用程序使用定位服务,这个方法会报告一个 kCLErrorDenied 错误。收到此类错误后,您应该停止定位服务。
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
if ([[error domain] isEqualToString: kCLErrorDomain] && [error code] == kCLErrorDenied)
// Location Services are denied.
【讨论】:
【参考方案2】:您应该检查CLLocationManager
的authorizationStatus
属性以查看您的应用程序是否有权访问位置信息 -
if ([CLLocationManager locationServicesEnabled] && (CLLoctionManager.authorizationStatus == kCLAuthorizationStatusAuthorized))
// Enable button
else
// Disable button
【讨论】:
【参考方案3】:使用这个
+(BOOL)checkLocationService
if (![CLLocationManager locationServicesEnabled])
return NO; //location service disabled
else if(kCLAuthorizationStatusAuthorized!=[CLLocationManager authorizationStatus])
return NO; //app's location service disabled
return YES;
【讨论】:
以上是关于位置服务行为怪异的主要内容,如果未能解决你的问题,请参考以下文章