id | title | brief |
---|---|---|
7074E47A-D2A3-4035-8BD3-57045E48B9D2 |
Create Geofence |
Geofences let you track a user's movement in and out of a geographical region. This recipe explains how to set up a simple geofence. |
Geofences work by specifying a circular region - CLCircularRegion
- and firing an event when the user crosses the CLCircularRegion
's boundary. A geofence subscribes to updates from the system's location manager, and will wake an application in the background to handle a boundary crossing event.
- Geofencing requires the CoreLocation library, so we'll start by adding the using directive:
using CoreLocation;
- Next, let's create an instance of the
CLLocationManager
class to interact with system location services:
- Starting with iOS 8, applications must call
requestWhenInUseAuthorization
onCLLocationManager
to gain access to the user's application. If the app hasn't been granted permission before, the user will be prompted to allow or deny access.
- In addition to explicitly requesting access to the user's location, you must add two keys to the Info.plist file, by opening Info.plist and selecting **Source**.
- Next, we'll create a
CLCircularRegion
to track. We define aCLCircularRegion
by specifying a center coordinate, a radius and a string identifier:
CLCircularRegion region = new CLCircularRegion(new CLLocationCoordinate2D(+37.29430502,-122.09423697), 10129.46, "Cupertino");
- Not all devices have geofencing enabled, so before we begin monitoring, we'll want to check if location monitoring is available by calling the
CLLocationManager
'sIsMonitoringAvailable
method, passing inCLCircularRegion
as theType
. We can subscribe to three updates from ourLocMgr
:RegionEntered
andRegionLeft
to track the user's movement, andDidStartMonitoringRegion
to let us know when a geofence has been established. The code example below shows us subscribing to all three events:
if (CLLocationManager.IsMonitoringAvailable (typeof(CLCircularRegion))) {
LocMgr.DidStartMonitoringForRegion += (o, e) => {
Console.WriteLine ("Now monitoring region {0}", e.Region.ToString ());
};
LocMgr.RegionEntered += (o, e) => {
Console.WriteLine ("Just entered " + e.Region.ToString ());
};
LocMgr.RegionLeft += (o, e) => {
Console.WriteLine ("Just left " + e.Region.ToString ());
};
LocMgr.StartMonitoring (region);
} else {
Console.WriteLine ("This app requires region monitoring, which is unavailable on this device");
}
- Once we've subscribed to events, we can call
StartMonitoring
on theLocMgr
, passing in aCLCircularRegion
, as illustrated by the code above. - To stop monitoring a region, call
StopMonitoring
on theLocMgr
:
if (CLLocationManager.IsMonitoringAvailable (typeof(CLCircularRegion))) {
LocMgr.StopMonitoring (region);
Console.WriteLine ("Stopped monitoring region: {0}", region.ToString ());
} else {
Console.WriteLine ("This app requires region monitoring, which is unavailable on this device");
}
- Running the application should produce the following output:
Now monitoring region CLCircularRegion (identifier:'Cupertino', center:<+37.29430502,-122.09423697>, radius:10129.46m)
Just entered CLCircularRegion (identifier:'Cupertino', center:<+37.29430502,-122.09423697>, radius:10129.46m)
Just left CLCircularRegion (identifier:'Cupertino', center:<+37.29430502,-122.09423697>, radius:10129.46m)
To test geofences in the iOS simulator, refer to the Test Location Changes in Simulator recipe.
While the code above will run in most situations, iOS location services require several more checks to ensure our geofencing application will run properly. First, we need to check if Location Services are enabled on the device by running CLLocationManager.LocationServicesEnabled
. Then, we should ensure that the user has authorized our application access to their location by checking the value of CLLocationManager.Status
, as illustrated by the code sample below:
if (CLLocationManager.LocationServicesEnabled) {
if (CLLocationManager.Status != CLAuthorizationStatus.Denied) {
if (CLLocationManager.IsMonitoringAvailable (typeof(CLCircularRegion))) {
LocMgr.StopMonitoring (region);
Console.WriteLine ("Stopped monitoring region: {0}", region.ToString ());
} else {
Console.WriteLine ("This app requires region monitoring, which is unavailable on this device");
}
LocMgr.Failed += (o, e) => {
Console.WriteLine (e.Error);
};
} else {
Console.WriteLine ("App is not authorized to use location data");
}
} else {
Console.WriteLine ("Location services not enabled, please enable this in your Settings");
}