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

时间:2018-04-19 09:11:54

标签: ios swift user-permissions

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

这是我的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)")
    }
}

2 个答案:

答案 0 :(得分:3)

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

的警报示例
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)

答案 1 :(得分:0)

您可以通过在您的位置管理器上调用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....