【问题标题】:Displaying location request prompt when user denies the location当用户拒绝位置时显示位置请求提示
【发布时间】:2017-03-26 00:11:33
【问题描述】:

当用户拒绝跟踪他的位置并且整个应用程序基于当前位置时,处理这种情况的正确方法是什么?我在 viewDidAppear 中调用了locationAuthorization(),当用户允许该位置时它工作正常。

func locationAuthorization(){
    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
        refreshData()

    } else if (CLLocationManager.authorizationStatus() == .denied) {
        addressButton.isHidden = true
    } else {
        locationManager.requestWhenInUseAuthorization()
        sleep(2)
        locationAuthorization()
    }
}

我试图在 .denied 语句中调用 locationAuthorization(),但它显然不起作用。输入locationManager.requestWhenInUseAuthorization() 也不起作用。 那么当用户点击“不允许”时,处理这种情况的最佳方法是什么?它使我的应用程序无法使用,因此我必须显示位置窗口,直到用户接受它。

【问题讨论】:

  • 您请求授权、休眠、然后再次调用locationAuthorization 的逻辑很糟糕。请不要那样做。
  • 而您需要妥善处理其他情况。仅在当前未知时请求授权。即使受到限制,您也在请求。这有点毫无意义。
  • @rmaddy 我知道睡眠不是最好的选择,但没有它就行不通。
  • 使用适当的委托方法在授权更改时被告知。
  • @rmaddy 感谢您的帮助。我使用 locationManager(... didChangeAuthorization) 然后添加警报以进入设置,现在它可以在没有睡眠和其他奇怪方法的情况下工作。

标签: swift core-location cllocationmanager


【解决方案1】:

把它放在.denied 部分:

let alert = UIAlertController(title: "Need Authorization", message: "This app is unusable if you don't authorize this app to use your location!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { _ in
    let url = URL(string: UIApplicationOpenSettingsURLString)!
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}))
self.present(alert, animated: true, completion: nil)

您不能再次请求授权,但您可以告诉人们在设置中进行更改。

【讨论】:

  • 我将“确定”按钮标记为“设置”,将“否”按钮标记为“取消”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多