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)")
}
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With