handling location permissions instantaneously in swift handling location permissions instantaneously in swift swift swift

handling location permissions instantaneously in swift


In order to do that, you need to implement the methoddidChangeAuthorizationStatus for your location manager delegate which is called shortly after CLLocationManager is initialized.

First, at the top of the file don't forget to add : import CoreLocation

To do that, in your class where you are using the location, add the delegate protocol. Then in the viewDidLoad method (or applicationDidFinishLaunching if you are in the AppDelegate) initialize your location manager and set its delegate property to self:

class myCoolClass: CLLocationManagerDelegate {    var locManager: CLLocationManager!    override func viewDidLoad() {        locManager = CLLocationManager()        locManager.delegate = self    } }

Finally, implement the locationManager(_ didChangeAuthorizationStatus _) method in the body of your class that you declared previously, this method will be called when the status of the authorization is changed, so as soon as your user clicked the button. You can implement it like this:

private func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {    switch status {    case .notDetermined:        // If status has not yet been determied, ask for authorization        manager.requestWhenInUseAuthorization()        break    case .authorizedWhenInUse:        // If authorized when in use        manager.startUpdatingLocation()        break    case .authorizedAlways:        // If always authorized        manager.startUpdatingLocation()        break    case .restricted:        // If restricted by e.g. parental controls. User can't enable Location Services        break    case .denied:        // If user denied your app access to Location Services, but can grant access from Settings.app        break    default:        break    }}

Swift 4 - New enum syntax

For Swift 4, just switch the first letter of each enum case to lowercase (.notDetermined, .authorizedWhenInUse, .authorizedAlways, .restricted and .denied)

That way you can handle each and every case, wether the user just gave its permission or revoked it.