在用户拒绝使用后,如何提示用户打开位置服务

我有一个明确的用户交互的应用程序,利用用户的当前位置。 如果用户拒绝访问位置服务,我仍然希望随后的使用提示用户转到设置并为我的应用重新启用位置服务。

我想要的行为是内置的地图应用程序的行为:

  1. 在设置>常规>重置>重置位置警告中重置位置警告。
  2. 启动地图应用
  3. 点击左下angular的当前位置button。
  4. 地图提示“”地图“将使用您当前的位置”| “不允许”| “允许”。
  5. select“不允许”选项。
  6. 再次点击左下angular的当前位置button。
  7. 地图提示“打开位置服务以允许”地图“确定您的位置”| “设置”| “取消”。

在我自己的应用程序中,相同的基本stream程会导致我的CLLocationManagerDelegate -locationManager:didFailWithError:方法在最后一步被调用kCLErrorDenied错误,并且用户无法打开“设置”应用程序进行更正。

为了响应这个错误,我可以显示自己的警报,但是无法启动“设置”应用程序,例如操作系统可以提供的内置地图应用程序所使用的警报。

CLLocationManager类中有什么我不知道能够给我这种​​行为吗?

使用iOS8,您最终可以通过openURL将用户链接到设置应用程序。 例如,您可以使用一个button来创build一个UIAlertView,将用户带到“设置”应用程序:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ICLocalizedString(@"LocationServicesPermissionTitle") message:ICLocalizedString(@"LocationPermissionGeoFenceMessage") delegate:self cancelButtonTitle:@"Settings" otherButtonTitles:nil]; [alert show]; 

在你的UIAlertView委托中:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString: UIApplicationOpenSettingsURLString]]; } 

更新:

从iOS 8开始,现在有一个常量的UIApplicationOpenSettingsURLString ,它代表一个URL,当打开它时,它会打开Settings应用程序到应用程序的设置(用户可以重新启用位置服务)。


原版的:

你没有办法做到这一点。 您唯一真正的select是显示警报,通知用户您的应用程序需要位置服务,并指示他们手动转到设置应用程序并将其打开。

AlertViews在iOS 8中弃用 。现在有一个更好的方法来使用新的AlertController来处理警报:

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString( @"Enter your title here", @"" ) message:NSLocalizedString( @"Enter your message here.", @"" ) preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"Cancel", @"" ) style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"Settings", @"" ) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]]; }]; [alertController addAction:cancelAction]; [alertController addAction:settingsAction]; [self presentViewController:alertController animated:YES completion:nil]; 

根据Apple的文档 locationServicesEnabled方法。

用户可以通过切换“常规”中的“位置服务”开关,从“设置”应用程序启用或禁用位置服务。

您应该在开始位置更新之前检查此方法的返回值,以确定用户是否为当前设备启用了位置服务。 如果此方法返回NO,并且始终启动位置更新,则核心位置框架将提示用户确认是否应重新启用位置服务。

所以你不能刚刚开始位置服务更新的任何方式来提醒警报?

以下是由Markus和bjc提供的代码的快速实现。

 let alertController = UIAlertController(title: NSLocalizedString("Enter your title here", comment: ""), message: NSLocalizedString("Enter your message here.", comment: ""), preferredStyle: .alert) let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil) let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .default) { (UIAlertAction) in UIApplication.shared.openURL(NSURL(string: UIApplicationOpenSettingsURLString)! as URL) } alertController.addAction(cancelAction) alertController.addAction(settingsAction) self.present(alertController, animated: true, completion: nil) 

我想你会在苹果考虑一个新的SDK时回答你的问题。 目前,据我所知,这是不可能的:

没有可用的URL处理程序
没有可用的方法来调用

但是…作为地图做,这可以做,但可能使用私人API。 如果你不害怕这种编码,你应该在我的意见中search。

这是Markus答案中的一个Swift版本的代码。 此代码创build一个警报,使用户可以select打开设置。

 let alertController = UIAlertController(title: NSLocalizedString("Enter your title here", comment: ""), message: NSLocalizedString("Enter your message here.", comment: ""), preferredStyle: .Alert) let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel, handler: nil) let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .Default) { (UIAlertAction) in UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!) } alertController.addAction(cancelAction) alertController.addAction(settingsAction) self.presentViewController(alertController, animated: true, completion: nil) 

Swift 3扩展创build设置警报控制器:

import基金会

 extension UIAlertController { func createSettingsAlertController(title: String, message: String) -> UIAlertController { let controller = UIAlertController(title: title, message: message, preferredStyle: .alert) let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment:"" ), style: .cancel, handler: nil) let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment:"" ), style: .default, handler: { action in UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!) }) controller.addAction(cancelAction) controller.addAction(settingsAction) return controller } }