如果用户第一次拒绝,如何再次请求权限(运行时)swift

How to ask permission (RunTime) again if the user deny for the first time swift

我拒绝首次启动应用程序的位置权限,当用户单击位置按钮时,我想显示请求权限的警告对话框。

这是我的 sudo 代码

点击位置按钮

if dont have location permission {
   ask for permission
} else {
  get current location
}

这是我的code.please查看我的评论code.Hope你提前了解我的problem.Thanks

@IBAction func getCurrentLocationButtonClick(_ sender: UIButton) {
///////////How to implement that sudo code/////////////////////////////
///////////Why This method is not calling for second time /////////////
//        locationManager.desiredAccuracy = kCLLocationAccuracyBest
//        locationManager.requestWhenInUseAuthorization()
//        locationManager.requestLocation()
/////////////////////////////////////////////////////
          locationManager.startUpdatingLocation()   
   }


override func viewWillAppear(_ animated: Bool) {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.requestLocation()
}

extension NotificationCenterViewController : CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

        /*

        if status == .denied {
            let alertController = UIAlertController(title: "Error", message: "We need your location.plese give permission", preferredStyle: .alert)

            let OKAction = UIAlertAction(title: "OK", style: .default) { (action) in
                //Ask for location permission
            }

            alertController.addAction(OKAction)
            self.present(alertController, animated: true)
        }

         */

        if status == .authorizedWhenInUse {
            locationManager.requestLocation()
            // locationManager.startLocationUpdates()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation: CLLocation = locations[0]
        locationManager.stopUpdatingLocation()
        let latitude = userLocation.coordinate.latitude
        let longitude = userLocation.coordinate.longitude
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
        print("error:: (error)")
    }
}

当您发现位置被拒绝时,您只能打开应用程序设置 这是带有

的警报示例
let alertController = UIAlertController (title: "Title", message: "Go to Settings?", 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
                print("Settings opened: \(success)") // Prints true
            })
        }
    }
    alertController.addAction(settingsAction)
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)

您通过在位置管理器上调用 requestWhenInUseAuthorization() 来请求许可。你的伪代码的问题是你不应该每次都这样做,而应该只做一次,之后用户应该去设置来更改权限。 CLLocationManager 拥有一个可以为此检查的权限状态。您可以在 IBAction

中执行类似的操作
let status = CLLocationManager.authorizationStatus()
if status == .notDetermined {
    locationManager.requestWhenInUseAuthorization()
    return
}

if status == .denied || status == .restricted {
    //Display alert that user needs to enable permissions in Settings 
    // or open settings directly as in the other answer
    showAlert()
    return
}

//Code to get location here....