用户拒绝位置服务后再次请求权限?

时间:2015-05-01 02:29:54

标签: ios iphone swift ios8 core-location

我跟踪用户的位置,并在我的负载首次加载时请求权限:

locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

如果用户拒绝,但后来通过在我的应用中启用配置选项改变了主意,我该如何再次询问?例如,我有一个自动检测用户位置的开关,所以当他们启用它时,我试图这样做:

@IBAction func gpsChanged(sender: UISwitch) {
    // Request permission for auto geolocation if applicable
    if sender.on {
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
}

但是这段代码似乎没有做任何事情。我希望它能再次询问用户是否允许应用程序跟踪用户的位置。这可能吗?

6 个答案:

答案 0 :(得分:47)

操作系统只会提示用户一次。如果他们拒绝允许,那就是它。您可以做的是通过将UIApplicationOpenSettingsURLString传递给UIApplication的{​​{1}}方法,将用户引导至您应用的设置。从那里,他们可以根据需要重新启用位置服务。也就是说,你可能不应该过于咄咄逼人地批评它们以获得许可。

答案 1 :(得分:8)

弹出的权限仅显示一次。 因此,之后我们必须将用户重定向到设置。 这是Swift中的代码:

 @IBAction func userDidClickButton(_ sender: Any) {

    // initialise a pop up for using later
    let alertController = UIAlertController(title: "TITLE", message: "Please go to Settings and turn on the permissions", preferredStyle: .alert)
    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }
        if UIApplication.shared.canOpenURL(settingsUrl) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in }
         }
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)
    alertController.addAction(settingsAction)

    // check the permission status
    switch(CLLocationManager.authorizationStatus()) {
        case .authorizedAlways, .authorizedWhenInUse:
            print("Authorize.")
            // get the user location
        case .notDetermined, .restricted, .denied:
            // redirect the users to settings
            self.present(alertController, animated: true, completion: nil)
    }
}

答案 2 :(得分:2)

你可以有另一种解决方案!您可以使用更好的消息显示自己的提醒,这可以说服您的用户允许接收应用的推送通知。如果用户允许,则只显示启用推送通知的默认权限警报,否则如果用户不允许,实际上不显示默认警报,您可以在数据库或NSUserDefaults中保存相应的标志,以后可以再次询问用户和再次在你的应用程序中的一些事件。

答案 3 :(得分:1)

你只有一次机会。要让用户在拒绝权限后启用权限,他们必须通过“设置”应用。请参阅CLLocationManager中请求使用位置服务的权限。

答案 4 :(得分:0)

我创建了一个包含用于通知的权限管理器的库,即使用户拒绝了权限,该库也可以处理权限警报。

https://github.com/CrazyPro007/PermissionManager/tree/master/PermissionManager/PermissionManager

答案 5 :(得分:0)

对于Health Kit api。开发人员可以简单地在readTypes Set<HKObjectType>中添加另一个权限,然后系统将再次提示用户以允许该权限。我不完全确定定位服务是否也能以同样的方式工作。

相关问题