繁体   English   中英

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

[英]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
}

这是我的代码。请检查我的代码中的注释。希望你能理解我的问题。谢谢。

@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....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM