当用户拒绝定位时显示定位请求提示

Displaying location request prompt when user denies the location

当用户拒绝跟踪他的位置并且整个应用程序都基于当前位置时,处理这种情况的正确方法是什么?我在 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() 也没有用。 那么当用户点击 "Don't allow" 时,最好的处理方式是什么?它使我的应用程序无法使用,所以我必须显示位置 window 直到用户接受它。

将其放在 .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)

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