Rah*_*diq 8 iphone cllocationmanager ios ios6
案例:对于当前位置,用户在应用安装上选择"不允许",那么有没有办法可以再次询问用户位置并触发当前位置的本机iphone警报?
我在stackoverflow上看过一些帖子,但是有旧的,现在有一个解决方案可以调用新的sdk或有人找到方法,
帖子提到: CLLocation再次请求许可
不幸的是你做不到.您可以做的一件事是提示用户更改位置设置.
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];
}
Run Code Online (Sandbox Code Playgroud)
在ios8中,apple引入了一个UIApplicationOpenSettingsURLString常量,它是设备"设置"视图的位置.
您可以编写以下代码(在swift中)以将用户定向到设置视图:
switch CLLocationManager.authorizationStatus() {
case .AuthorizedWhenInUse, .Restricted, .Denied:
let alertController = UIAlertController(
title: "Background Location Access Disabled",
message: "In order to be notified, please open this app's settings and set location access to 'Always'.",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
似乎接受的答案并非完全正确.[CLLocationManager locationServicesEnabled]检查是否已启用位置服务,如文档中所述.
返回一个布尔值,指示是否在设备上启用了位置服务.用户可以通过切换"常规"中的"位置服务"开关,从"设置"应用程序启用或禁用位置服务.您应该在开始位置更新之前检查此方法的返回值,以确定用户是否为当前设备启用了位置服务.位置服务在用户第一次尝试在应用程序中使用与位置相关的信息时会提示用户,但不会提示后续尝试.如果用户拒绝使用位置服务并且您仍尝试启动位置更新,则位置管理器会向其委托报告错误.
如果您想检查是否允许用户使用他/她的位置,您应该检查[CLLocationManager authorizationStatus].如果您的应用的状态为kCLAuthorizationStatusDenied,则表示用户在要求获得权限时明确拒绝了您的应用.您可以使用它并相应地通知用户.
| 归档时间: |
|
| 查看次数: |
8528 次 |
| 最近记录: |