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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29980832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:50:13  来源:igfitidea点击:

Request permissions again after user denies location services?

iosiphoneswiftios8core-location

提问by TruMan1

I track the user's location and ask for permission when my load first loads using this:

我跟踪用户的位置并在我的负载首次加载时请求许可:

locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

If the user denies, but later changes their mind by enabling the configuration option in my app, how do I ask again? For example, I have a switch for auto detecting the user's location so when they enable it, I am trying to do this:

如果用户拒绝,但后来通过在我的应用程序中启用配置选项改变了主意,我该如何再次询问?例如,我有一个用于自动检测用户位置的开关,因此当他们启用它时,我正在尝试执行以下操作:

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

But this code doesn't seem to do anything. I was hoping it would ask the user again if they want to allow the app to track the user's location. Is this possible?

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

回答by bgilham

The OS will only ever prompt the user once. If they deny permission, that's it. What you cando is direct the user to the Settings for your app by passing UIApplicationOpenSettingsURLStringto UIApplication's openURL:method. From there, they can re-enable location services if they wish. That said, you probably shouldn't be too aggressive about bugging them for the permission.

操作系统只会提示用户一次。如果他们拒绝许可,就是这样。您可以做的是通过传递UIApplicationOpenSettingsURLStringtoUIApplicationopenURL:方法将用户定向到您的应用程序的设置。从那里,他们可以根据需要重新启用定位服务。也就是说,您可能不应该过于积极地窃听他们的许可。

回答by Allen

The permission pop up only shows once. So we have to redirect users to Settingsafter that. Here comes the code in Swift:

权限弹出只显示一次。所以我们必须在那之后将用户重定向到设置。下面是 Swift 中的代码:

 import CoreLocation 
 @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)
    }
}

回答by Khushabu

You can have an alternate solution!! You can show your own alert with the better message which can convince your user to allow to receive push notifications for your app. If user allows, then only you show default permission alert for enable push notification otherwise if user disallows, don't show default alert in-fact, you can save corresponding flag in your database or NSUserDefaults and can ask user later on again and again on some events in your app.

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

回答by Connor

You only get one chance. For the user to enable permissions after denying them, they have to go through the Settings app. See Requesting Permission to Use Location Services in CLLocationManager.

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

回答by CrazyPro007

I have created the library that include permission manager for notification which handle the permission alert even after user decline the permission.

我创建了包含通知权限管理器的库,即使在用户拒绝权限后,它也会处理权限警报。

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

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

回答by Harvey Connor

For the Health Kit api. Developers can simply add another permission into the readTypes Set<HKObjectType>and the user will be re-prompted to allow permissions. I'm not entirely sure if this works the same way with location services.

对于 Health Kit api。开发人员可以简单地添加另一个权限readTypes Set<HKObjectType>,用户将被重新提示允许权限。我不完全确定这是否与位置服务相同。