Get location updates in the most power-efficient way, but less frequently than with other services.
Framework
- Core Location
Overview
The visits service is the most power-efficient way of gathering location data. With this service, the system delivers location updates only when the user’s movements are noteworthy. Each update includes both the location and the amount of time spent at that location. This service is not intended for navigation or other real-time activities, but you can use to identify patterns in the user’s behavior and apply that knowledge to other parts of your app. For example, a music app might prepare a gym playlist if a user arrives at the gym at their normal workout time.
Note
The visits location service requires always authorization. For information about how to request authorization, see Requesting Always Authorization.
To start the visits location service, call the start
method of your location manager. With this service, the location manager ignores the values in its distance
and desired
properties, so you do not need to configure them. The location manager delivers updates to the location
method of its delegate. Listing 1 shows how to check for the availability of the visits location service and start it.
Starting the visits location service
func startReceivingVisitChanges() {
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedAlways {
// User has not authorized access to location information.
return
}
if !CLLocationManager.locationServicesEnabled() {
// This service is not available.
return
}
locationManager.startMonitoringVisits()
}
Another way to save power is to set the pauses
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 activity
property of your location manager.
Note
When you no longer need location data, always call your location manager object's stop
method. If you do not stop location updates, the system continues delivering location data to your app, which could drain the battery of the user's device.
Receiving Visit Updates
When you start the visits location service, a recently cached value may be reported to your delegate immediately. As new visit data is obtained, the location manager calls your delegate's location
method with the updated value.
Receiving visit updates
func locationManager(_ manager: CLLocationManager, didVisit visit: CLVisit) {
// Do something with the visit.
}
Handling Location-Related Errors
When the location manager is unable to deliver location updates, it calls the location
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.
Stopping location services when authorization is denied
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
if error.code == .denied {
// Location updates are not authorized.
locationManager.stopMonitoringVisits()
return
}
// Notify the user of any errors.
}