I am creating a location based app, and when the user has denied location access an alert view pops up asking the user go to the settings page and update the settings to allow location access. If the user allows access and then presses back the view does not refresh with the updated settings allowing the update of location.
Is there a function that can be run after the user comes back into the app from the settings page that will refresh and start updating location.
I appreciate any advice or help. Thank you.
USER SEES ALERT:

AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:

func alertForNoLocationAccess(){
let alert = UIAlertController(title: "Allow Location Access", message: "Cannot add location data and map coordinates to your entries without access. Press settings to update or cancel to deny access.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { action in
if let url = URL(string:UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
Register ViewController/AppDelegate to NotificationCenter to notify when user has opened the app from Settings.
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
On user opening the app, request for location updates again to CLLocationManager. If user has accepted the request already then CLLocationManager will start updating user's location without interrupting.
@objc func willEnterForeground() {
//Register for
registerForLocationUpdates()
}
func registerForLocationUpdates() {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
Check Apple's documentation, and you find this:
"When you request authorization, or when your app's authorization status changes, use the locationManager(_:didChangeAuthorization:) method of your delegate object to process the changes. Listing 3 shows an implementation of that method that enables or disables features based on the app's current authorization level."
Obviously this delegate method will also be triggered if the user decides to turn off your permission to get location data.
If you are showing this alert, you should also use this notification to dismiss the alert, in case the user went to Settings manually without using the button in your alert.
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