Article

Handling Location Events in the Background

Take advantage of background execution modes to respond to location-related events at any time.

Overview

Most location services are meant to be used while your app is in the foreground, but some can also run in the background or even cause your app to be launched. To receive events in the background, configure the Location updates capability for your app in Xcode, as shown in Figure 1. You must also set the allowsBackgroundLocationUpdates property of your CLLocationManager object to true.

Figure 1

Enabling background location updates

The capabilities pane in which you enable background location updates

When the system launches your app, use the launch options dictionary passed to the application(_:willFinishLaunchingWithOptions:) and application(_:didFinishLaunchingWithOptions:) methods to determine whether your app was launched to handle a location update. This dictionary contains the location key when the app is launched because of location services. Create a new CLLocationManager object, configure it with a delegate, and start location services again to receive the update.

Table 1 lists the services that launch your app to report new location updates. Services that cause an app to be relaunched do so even after the user force-quits the app

Table 1

Services that relaunch your app after it has been terminated

Service

Launches app

Standard location service

No

Significant-change location service

Yes

Visits service

Yes

Region monitoring

Yes

iBeacon ranging

No

Heading service

No

See Also

Related Topics

Using the Visits Location Service

Get location updates in the most power-efficient way, but less frequently than with other services.

Using the Significant-Change Location Service

Get location updates in a power-friendly way, but less frequently than with the standard location service.

Using the Standard Location Service

Get regular location updates based on the parameters you specify.