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

mik*_*098 3 core-location cllocationmanager swift

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

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

    } else if (CLLocationManager.authorizationStatus() == .denied) {
        addressButton.isHidden = true
    } else {
        locationManager.requestWhenInUseAuthorization()
        sleep(2)
        locationAuthorization()
    }
}
Run Code Online (Sandbox Code Playgroud)

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

pap*_*111 7

将其放在.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)
Run Code Online (Sandbox Code Playgroud)

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