简体   繁体   English

如何检查位置服务是否打开?

[英]How to check if Location Services is On or not?

How can I check if the user has turned off Location Services ? 如何检查用户是否已关闭位置服务?

So that I can prompt him/her to turn it On in order to use my app. 这样我就可以提示他/她打开它以便使用我的应用程序。

Thank you ! 谢谢 !

The CLLocationManager provides class methods to determine the availability of location services: CLLocationManager提供类方法来确定位置服务的可用性:

- (BOOL)locationServicesEnabled (for < iOS 4.0)

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

+ (CLAuthorizationStatus)authorizationStatus (for iOS 4.2+)

(and others, see documentation) (和其他人一样,见文件)

If your application absolutely cannot run without Location Services, then you can make Location Services a requirement for installing/running your app using the app's Info.plist. 如果您的应用程序绝对无法在没有位置服务的情况下运行,那么您可以使用应用程序的Info.plist将位置服务作为安装/运行应用程序的要求。 You do this by adding the UIDeviceCapabilities key to your app's Info.plist and giving it a corresponding value of "location-services" minus the quotes. 您可以通过将UIDeviceCapabilities键添加到应用程序的Info.plist并为其提供相应的“location-services”值减去引号来实现此目的。

With the Info.plist configured this way, if Location Services are turned off, or if the device is in airplane mode, or anything else is preventing the use of Location Services on the device, iOS will prompt the user to turn on Location Services when the app is opened. 使用这种方式配置Info.plist,如果关闭位置服务,或者设备处于飞行模式,或者其他任何东西阻止在设备上使用位置服务,iOS将提示用户打开位置服务时该应用程序已打开。

EDIT: Brief experimentation seems to indicate that iOS does not prompt the user in this circumstance, so this would not be a good solution for you. 编辑:简短的实验似乎表明iOS在这种情况下不会提示用户,所以这对你来说不是一个好的解决方案。

For more information, you can look at the Information Property List Key Reference section of Apple's developer documentation. 有关更多信息,请查看Apple开发人员文档的“信息属性列表主要参考”部分。

Use the below piece of code... 使用下面的代码......

  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];

}

Use the following code, which will work even in iOS 8. 使用以下代码,即使在iOS 8中也能使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM