Use region monitoring to determine when the user enters or leaves a geographic region.
- Core Location
Region monitoring (also known as geofencing) is a way for your app to be alerted when the user enters or exits a geographical region. You might use region monitoring to perform location-related tasks. For example, the Reminders app uses them to trigger reminders when the user arrives at or leaves a specified location, as shown in Figure 1.
In iOS, regions are monitored by the system, which wakes up your app as needed when the user crosses a defined region boundary. In macOS, region monitoring works only while the app is running (either in the foreground or background) and the user’s system is awake. The system does not launch Mac apps to deliver region-related notifications.
Define and Monitor a Geographic Region
A region is a circular area centered on a geographic coordinate, and you define one using a
CLCircular object. The radius of the region object defines its boundary. You define the regions you want to monitor and register them with the system by calling the
start method of your
CLLocation object. The system monitors your regions until you explicitly ask it to stop or until the device reboots.
Listing 1 shows how to configure and register a region centered around a point provided by the caller of the method. The method uses the largest allowed radius to define the boundaries of the region and asks that the system deliver notifications only when the user enters the region.
Handle a Region-Related Notification
Whenever the user crosses the boundary of one of your app's registered regions, the system notifies your app. If an iOS app is not running when the boundary crossing occurs, the system tries to launch it. An iOS app that supports region monitoring must enable the Location updates background mode so that it can be launched in the background.
Boundary crossing notifications are delivered to your location manager's delegate object. Specifically, the location manager calls the
location methods of its delegate. If your app was launched, you must configure a
CLLocation object and delegate object right away so that you can receive these notifications. To determine whether your app was launched for a location event, look for the
UIApplication in the launch options dictionary.
When determining whether a boundary crossing happened, the system waits to be sure before sending the notification. Specifically, the user must travel a minimum distance over the boundary and remain on the same side of the boundary for at least 20 seconds. These conditions help eliminate spurious calls to your delegate object’s methods.
Listing 2 shows a delegate method that is called when the user enters a registered region. Regions have an associated identifier, which this method uses to look up information related to the region and perform the associated action.
Termination of your app (by the user or the system) does not prevent it from being relaunched to handle region boundary crossings. However, when Background App Refresh is disabled, either for your app or for all apps, the user must explicitly launch your app to resume the delivery of all location-related events.