Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I deny location permission first app launch and when user click the location Button i want to display alert dialog asking for permission.

Here is my sudo code

Click location Button

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

Here is my code.please check the comments in my code.Hope you understand my problem.Thanks in advance.

@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)")
    }
}
like image 223
John Avatar asked Apr 19 '18 09:04

John


1 Answers

When you discover that location is denied, you only can open app settings here is example of alert with that

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)
like image 190
Lu_ Avatar answered Oct 06 '22 00:10

Lu_