【发布时间】:2011-06-22 07:26:33
【问题描述】:
如何检查用户是否关闭了定位服务?
以便我可以提示他/她打开它以使用我的应用程序。
谢谢!
【问题讨论】:
-
你应该在apple.stackexchange.com上问这个问题,你可能会很快得到答复。
如何检查用户是否关闭了定位服务?
以便我可以提示他/她打开它以使用我的应用程序。
谢谢!
【问题讨论】:
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...
如果您的应用程序在没有定位服务的情况下绝对无法运行,那么您可以使用应用程序的 Info.plist 将定位服务作为安装/运行应用程序的要求。为此,您可以将 UIDeviceCapabilities 键添加到应用的 Info.plist 中,并为其赋予相应的“location-services”值减去引号。
以这种方式配置 Info.plist 后,如果位置服务已关闭,或者设备处于飞行模式,或者有任何其他因素阻止设备上使用位置服务,iOS 将提示用户打开打开应用时的定位服务。
编辑:简短的实验似乎表明 iOS 在这种情况下不会提示用户,所以这对你来说不是一个好的解决方案。
有关更多信息,您可以查看 Apple 开发者文档的 Information Property List Key Reference 部分。
【讨论】:
使用下面的代码...
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];
}
【讨论】:
使用以下代码,即使在 iOS 8 中也可以使用。
if([CLLocationManager locationServicesEnabled]&&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
//...Location service is enabled
}
else
{
//...Location service is disabled
}
【讨论】: