Article

Using the Significant-Change Location Service

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

Overview

The significant-change location service offers a more power-friendly alternative for apps that need location data but do not need frequent updates or the precision of GPS. The service relies on lower-power alternatives (such as Wi-Fi and cellular information) to determine the user’s location. It then delivers location updates to your app only when the user’s position changes by a significant amount, such as 500 meters or more.

To start the significant-change location service, call the startMonitoringSignificantLocationChanges method of your location manager. With this service, the location manager ignores the values in its distanceFilter and desiredAccuracy properties, so you do not need to configure them. The location manager delivers updates to the locationManager:didUpdateLocations: method of its delegate. Listing 1 shows how to check for the availability of the significant-change location service and start it.

Listing 1

Starting the significant-change location service

func startReceivingSignificantLocationChanges() {
    let authorizationStatus = CLLocationManager.authorizationStatus()
    if authorizationStatus != .authorizedAlways {
        // User has not authorized access to location information.
        return
    }
    
    if !CLLocationManager.significantLocationChangeMonitoringAvailable() {
        // The service is not available.
        return
    }
    locationManager.delegate = self
    locationManager.startMonitoringSignificantLocationChanges()
}

Another way to save power is to set the pausesLocationUpdatesAutomatically property of your location manager object to true. Enabling this property lets the system reduce power consumption by disabling location hardware when the user is unlikely to be moving. Pausing updates does not diminish the quality of those updates, but can improve battery life significantly. To help the system determine when to pause updates, you must also assign an appropriate value to the activityType property of your location manager.

Receiving Location Updates

When you start the significant-change location service, a recently cached value may be reported to your delegate immediately. As new location data is obtained, the location manager calls your delegate's locationManager:didUpdateLocations: method with the updated values. The locations parameter always contains at least one location and may contain more than one. Locations are always reported in the order in which they were determined, so the most recent location is always the last item in the array, as shown in Listing 2.

Listing 2

Processing the most recent location update

func locationManager(_ manager: CLLocationManager,  didUpdateLocations locations: [CLLocation]) {
   let lastLocation = locations.last!
               
   // Do something with the location. 
}

Before using a location value, check the time stamp of the CLLocation object. Because the system may return cached locations, checking the time stamp lets you know whether to update your interface right away or perhaps wait for a new location.

Handling Location-Related Errors

When the location manager is unable to deliver location updates, it calls the locationManager:didFailWithError: method of its delegate object. You should always implement this delegate method to handle any errors that might occur. For example, this method is called when the user denies authorization for your app to use location services. In such a scenario, you might want to disable location-related features and notify the user which features are unavailable. You should also stop whatever service you previously started, as demonstrated in Listing 3.

Listing 3

Stopping location services when authorization is denied

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
   if let error = error as? CLError, error.code == .denied {
      // Location updates are not authorized.
      manager.stopMonitoringSignificantLocationChanges()
      return
   }       
   // Notify the user of any errors.
}

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 Standard Location Service

Get regular location updates based on the parameters you specify.

Handling Location Events in the Background

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