Implement features that can access information about a user’s location.
Framework
- Core Location
Overview
To add location services to your app, you use CLLocation
, implement its delegate, and decide the authorization mode your app requires. When your app runs, it should check if the device supports location services, configure and start the desired location services, and request authorization to receive the user’s location. Once authorized, your app receives location events for the Core Location services it requests.
Determine If the User's Device Supports Location Services
Core Location services require hardware that may not be present on every device. Before relying on any specific location service, check if the device supports the service by calling the methods of the CLLocation
object listed in Table 1. If a method returns false
, the device does not support that particular service.
Methods for determining if devices support Core Location services
Method | Description |
---|---|
Indicates whether the device supports getting a user's location with the significant-change location service. | |
Indicates whether the device supports region monitoring to detect entry into or exit from geographic regions or beacon regions. | |
Indicates whether the device is able to provide compass-related headings. | |
Indicates the device supports obtaining the relative distance to a nearby iBeacon device. |
Note
If your app can't function on a device that lacks a specific hardware-dependent capability, indicate the requirement of that service in your app's Info
. For more information, see Requiring the Presence of Specific Location Services.
Create the Location Manager and Delegate
Create an instance of the CLLocation
class and store a strong reference to it somewhere in your app. You must keep a strong reference to the location manager object until all tasks involving that object complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient.
Assign a custom object to the delegate
property, conforming to the CLLocation
protocol. Assign the delegate before starting any location services. The system calls your delegate object’s methods from the thread in which you started the corresponding location services. That thread must itself have an active run loop, like the one found in your app's main thread.
Handle Errors in the Delegate Methods
Implement the failure-related methods in the delegate to fail gracefully when location services are not available on a device. If you try to start an unavailable service, the CLLocation
object calls one of the failure-related methods of its delegate. For example, if region monitoring is unavailable, the object calls the location
method. You may want to update the UI in your app when specific location services are not available.
Ask for Authorization and Handle Changes
Determine the authorization status your app needs. Ask for authorization to use location services when your users access location related functionality in your app. See Choosing the Location Services Authorization to Request for more information.
Implement your delegate’s location
, which the system calls as soon as your app creates the location manager, and any time your app’s authorization status changes. Use this method to respond to changes in your app’s authorization status and perform the appropriate actions for each state. For example, you may want to turn on or off your app’s location features as appropriate when authorization changes.
Start Location Services and Receive Events
You must set the delegate object before you use other methods on your CLLocation
instance. Next, you must:
Call the appropriate method in
CLLocation
to start the delivery of events.Manager Receive location and heading related updates in the associated delegate object.
Call the appropriate method in
CLLocation
to stop the service when your app no longer needs to receive the location events.Manager
For the services you use, configure any properties associated with that service accurately. Core Location manages power aggressively by turning off hardware when it’s not needed. For example, setting the desired accuracy for location events to one kilometer gives the location manager the flexibility to turn off GPS hardware and rely solely on the WiFi or cell radios, which can lead to significant power savings.