簡體   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