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

问题描述 投票:20回答:5

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

locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

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

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

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

ios iphone swift ios8 core-location
5个回答
48
投票

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


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
投票

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


1
投票

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


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.