React to location authorization changed in Settings
Observing authorization changes via the delegate.
Implement locationManagerDidChangeAuthorization, which fires on launch and whenever status changes, read authorizationStatus, and start updates when authorized.
WHAT THIS TESTS This evaluates whether you know the modern Core Location authorization-change delegate and understand that a suspended app cannot run code, so the callback arrives when the app becomes active again with a live manager.
A GOOD ANSWER COVERS You set a CLLocationManagerDelegate and implement locationManagerDidChangeAuthorization(_:), introduced to replace the older locationManager(_:didChangeAuthorization:) that passed a status argument. The system calls this method once when you first create and assign a manager, and again every time the authorization status changes. Crucially, while the app is truly suspended it cannot execute, but as soon as it is resumed or relaunched and you create or already hold a CLLocationManager with its delegate set, the delegate fires reflecting the new status the user chose in Settings. In the callback you read manager.authorizationStatus, and if it is authorizedWhenInUse or authorizedAlways you immediately call startUpdatingLocation() or requestLocation() to begin monitoring.
COMMON WRONG ANSWERS Manually polling authorizationStatus in viewDidAppear instead of relying on the delegate. Using the deprecated status-argument method. Believing code can run while the app is suspended in the background. Forgetting that the delegate must be set before the change to receive the initial callback.
LIKELY FOLLOW-UPS Why authorizationStatus is now an instance property rather than a class method. How accuracyAuthorization conveys full versus reduced precision. What happens with provisional or temporary authorization. How to re-prompt or deep link to Settings using UIApplication.openSettingsURLString when status is denied.
ONE CONCRETE EXAMPLE Your manager has delegate set at app launch. The user previously denied access, so status was denied. They background your app, open Settings, and switch your location to While Using. When they return and the app becomes active, locationManagerDidChangeAuthorization fires. You read manager.authorizationStatus, see authorizedWhenInUse, and call manager.startUpdatingLocation(), so monitoring begins immediately without the user taking any further action inside your app.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.