16.5. Handling Location Changes in the Background
Problem
You are writing an application whose main functionality is processing location changes using Core Location. You want the application to retrieve the iOS device location changes even if the application is sent to the background.
Solution
Select your project file in the Navigator of Xcode. Then, from the Capabilities section, switch on the Background Modes subsection. After the list of background modes is given to you, tick on the Location switch.
Discussion
When your application is running in the foreground, you can
receive delegate messages from an instance of CLLocationManager telling you when iOS detects
that the device is at a new location. However, if your application is
sent to the background and is no longer active, the location delegate
messages will not be delivered normally to your application. They will
instead be delivered in a batch when your application again becomes the
foreground application.
If you still want to be able to receive changes in the location of the user’s device while running in the background, you must enable the Location updates capability of your app, as described in the Solution section of this recipe. Once in the background, your application will continue to receive the changes in the device’s location. Let’s test this in a simple app with just the app delegate.
What I intend to do in this app is to keep a Boolean value in the
app delegate, called executingInBackground. When the app goes to the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access