2011-02-06 71 views
6

如何检查用户是否已关闭位置服务?如何检查位置服务是否打开?

为了使用我的应用程序,我可以提示他/她打开它。

谢谢!

+0

你要问这对http://apple.stackexchange.com/可能是在那里你会得到答案快。 – 2011-02-06 15:59:24

回答

13

CLLocationManager提供类方法来确定位置服务的可用性:

- (BOOL)locationServicesEnabled (for < iOS 4.0)

+ (BOOL)locationServicesEnabled (for iOS 4.0 and greater)

+ (CLAuthorizationStatus)authorizationStatus (for iOS 4.2+)

(和其他人,看文档)

+0

这个属性是我一直在寻找!谢谢! =) – nosuic 2011-02-06 16:20:10

3

如果您的应用程序绝对不能运行没有定位服务,那么您可以使位置服务的安装/运行应用程序的使用应用程序的Info.plist的要求。您可以通过将UIDeviceCapabilities关键字添加到应用程序的Info.plist中,并为其添加相应的“location-services”值减去引号。

通过这种方式配置Info.plist,如果定位服务关闭,或者设备处于飞行模式,或者其他任何设置阻止在设备上使用定位服务,iOS会提示用户转向在应用程序打开时位置服务。

编辑:简短的实验似乎表明,iOS在这种情况下不提示用户,所以这不会是一个很好的解决方案。

有关更多信息,请参阅Apple开发人员文档的信息属性列表关键参考部分。

+0

非常感谢您的信息! – nosuic 2011-02-06 16:19:27

1

使用以下一段代码...

if (![CLLocationManager locationServicesEnabled]) { 


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 

} 
0

使用下面的代码,这将工作,即使在iOS的8

if([CLLocationManager locationServicesEnabled]&& 
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) 
{ 
    //...Location service is enabled 
} 
else 
{ 
//...Location service is disabled 
} 
相关问题