2011-02-03 82 views
65

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

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

  1. 在设置复位位置警告>通用>还原>还原位置警告。
  2. 启动地图应用程序。
  3. 点击左下角的当前位置按钮。
  4. 带“”地图“的地图提示将使用您当前的位置”| “不允许”| “允许”。
  5. 选择“不允许”选项。
  6. 再次点击左下角的当前位置按钮。
  7. 将地图提示与“打开位置服务以允许”地图“确定您的位置”| “设置”| “取消”。

在自己的应用程序,同样的基本流程的结果在我的CLLocationManagerDelegate -locationManager:didFailWithError:方法被调用了kCLErrorDenied错误,在最后步骤,并且用户没有给出,打开设置应用程序来纠正选项它。

我可以显示自己的警报来响应错误,但它不能启动设置应用程序,就像操作系统可以提供内置地图应用程序所使用的警报一样。

CLLocationManager类中有什么我缺少能够给我这种​​行为吗?

+0

现在,我只是显示一个警告用户,要求他们去设置以重新启用它。我也很想听到更好的解决方案。 – donkim 2011-02-03 23:22:13

+0

我也想回答这个问题,当然还有更好的方法 – conorgriffin 2011-02-21 19:46:58

+0

我发现CoreLocation并不满足这个原因。我最终使用了容易整合和有据可查的skyhook库。坐标似乎也更精确。唯一的缺点是必须将1.5MB dylib与应用程序捆绑在一起。 – 2011-03-17 13:32:50

回答

39

使用iOS8,您最终可以通过openURL将用户链接到设置应用程序。例如,您可以创建一个单一的按钮,用户将进入到设置应用一个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]]; 
} 
2

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

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

但是......随着地图这样做,这是可以做到的,但可能使用私人API。如果你不害怕这种编码,你应该在我的意见中搜索。

31

更新:

由于iOS 8的的,现在有不断UIApplicationOpenSettingsURLString,其表示,打开时,打开设置应用到你的应用程序的设置(用户可以重新启用位置的URL服务)。


原文:

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

+0

我已阅读过Apple文档,并根据文档发布了以下答案,但我没有对自己进行测试。这是SDK中的变化还是相同? – 2012-07-22 17:49:36

+0

恐怕这个答案不再正确。你可能希望更新或删除你的答案。 – 2015-07-10 12:59:57

6

根据Apple's DocslocationServicesEnabled方法。

The user can enable or disable location services from the Settings application by toggling the Location Services switch in General.

You should check the return value of this method before starting location updates to determine whether the user has location services enabled for the current device. If this method returns NO and you start location updates anyway, the Core Location framework prompts the user to confirm whether location services should be reenabled.

所以你不能刚刚开始位置服务更新任何方式导致警报提示?

16

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

斯威夫特3扩展创建设置报警控制器:

进口基金会

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 
    } 
} 
1

下面的代码在由马库斯回答迅捷的版本。此代码会创建一个警报,使用户可以选择打开设置。

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) 
5

这是由Markus和bjc提供的代码的swift 3实现。

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) 
1

在Swift 4中,它的语法有一个更新。

斯威夫特4

extension UIAlertController { 

    func createSettingsAlertController(title: String, message: String) -> UIAlertController { 

     let alertController = 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) { (UIAlertAction) in 
     UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)! as URL, options: [:], completionHandler: nil) 
     } 

     alertController.addAction(cancelAction) 
     alertController.addAction(settingsAction) 
     self.present(alertController, animated: true, completion: nil) 

    } 
} 
相关问题