【问题标题】:How to check if Location Services is On or not?如何检查定位服务是否开启?
【发布时间】:2011-06-22 07:26:33
【问题描述】:

如何检查用户是否关闭了定位服务?

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

谢谢!

【问题讨论】:

标签: iphone service location


【解决方案1】:

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

- (BOOL)locationServicesEnabled (for < iOS 4.0)

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

+ (CLAuthorizationStatus)authorizationStatus (for iOS 4.2+)

(以及其他,请参阅文档)

【讨论】:

  • 这个属性就是我要找的!谢谢! =)
  • 注意authorizationStatus 需要iOS4.2+ 和+ (BOOL)locationServicesEnabled 需要iOS4.0...而对于以前的iOS 版本,它是- (BOOL)locationServicesEnabled...
【解决方案2】:

如果您的应用程序在没有定位服务的情况下绝对无法运行,那么您可以使用应用程序的 Info.plist 将定位服务作为安装/运行应用程序的要求。为此,您可以将 UIDeviceCapabilities 键添加到应用的 Info.plist 中,并为其赋予相应的“location-services”值减去引号。

以这种方式配置 Info.plist 后,如果位置服务已关闭,或者设备处于飞行模式,或者有任何其他因素阻止设备上使用位置服务,iOS 将提示用户打开打开应用时的定位服务。

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

有关更多信息,您可以查看 Apple 开发者文档的 Information Property List Key Reference 部分。

【讨论】:

  • 非常感谢您提供的信息!
【解决方案3】:

使用下面的代码...

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

}

【讨论】:

    【解决方案4】:

    使用以下代码,即使在 iOS 8 中也可以使用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-22
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      相关资源
      最近更新 更多