Tech Blogs

perspectives for IT decision makers

‘Geofencing’: Using Core Location for Regional Monitoring in iOS Applications

by (August 26, 2014)

Geofencing-for-iOS

 

A ‘Geofence’ is a geographical area defined by a circle of a specified radius around a known point on the Earth’s surface. More specifically, a geofence is a virtual barrier, set around a specified centre and a radius.Any program that incorporates geofencing feature, is likely to respond to the user’s entry and exit across a specified boundary. It is the administrators’ privilege on how they can utilize the response in order to suit the requirement.

Geofencing in iOS Applications

To incorporate Geofencing in iOS Apps, we seek the help of ‘Core Location’ Framework. The Core Location framework provides two ways to detect a user’s entry and exit for specific regions: geographical region monitoring (iOS 4.0 and later and OS X v10.8 and later) and beacon region monitoring (iOS 7.0 and later). Here, we are talking about the geographical region monitoring. In iOS, geofences are always being tracked, even if the app isn’t running. When an app crosses a boundary of any geofence, it is being monitored. The application will be re-launched into the background to handle the response. The developer will have all the liberty to handle the response.

The Requirements

Before attempting to monitor any region, it is important to make sure that the current device is supporting region monitoring. The some reasons and circumstances due to which region monitoring might not be supported are:

•  The device may not have the hardware that supports region monitoring.
•  The user might have denied the application’s authorization to use the region monitoring feature.
•  The user might have disabled the ‘location services’ or ‘GPS support’ in the device settings.
•  The user might have disabled ‘Background App Refresh’ in device settings – either for the device or particularly for the application.
•  The device might be in Airplane/Offline mode so that the hardware may not exhaust power.

But there are solutions, iOS possess classes and methods that help the developer to determine the availability of region monitoring. If the application is supposed to work in the background as well, it is recommended to check the background ‘RefreshStatus’ property of the UIApplication class, to process the location updates in the background.

The Integration

To start with the monitoring of a region, we must define the region and register it within the system. In iOS 7.0 or the later versions, ‘CLCircularRegion’ Class is used to define the region. Region definition must include the data related to the geographical area of the region, which is the centre and also, the desired radius around the centre of the region. The definition must also include an identifier for the region that uniquely distinguishes each region. The identifier string is the only guaranteed weapon that your app has to identify a region later.

To register a region, you just need to call the ‘startMonitoringForRegion’ method of ‘CLLocationManager’ object.
Monitoring of regions begins immediately after the registration. Region crossing events are handled by 2 delegate methods. As soon as the app enters a registered region, location manager’s ‘-locationManager:didEnterRegion:’ method will be triggered. Similarly, when the app exits a registered region, ‘-locationManager:didExitRegion:’ method will be triggered. The identifier string is used to identify the region which made the delegate trigger.

When the region monitoring fails for any region or any error occurs, location manager will notify by triggering ‘-locationManager:monitoringDidFailForRegion:withError:’

Getting Initial GeoFence

One limitation, of the iOS Geofencing API is that it will respond only to the boundary crossing events. If the user/application is already inside the geofence at the time of registration, the ‘CLLocationManager’ does not generate any event. Either the application must wait until the user crosses the boundary to catch the event or the administrator has to handle this condition separately. In iOS7, we can use ‘requestStateForRegion’: method of the ‘CLLocationManager’ class to check whether the user is already inside a fence.

When the app gets launched, the location manager checks the state of all the regions that are registered, and the ‘didDetermineState:forRegion’ method of ‘CLLocationManager’ will be triggered, which determines the state for each region. The state of the region will fall under any 3 categories of CLRegionState – CLRegionStateUnknown, CLRegionStateInside, and CLRegionStateOutside. If Region State is equal to CLRegionStateInside, it means that, the region is already inside the geofence.

To Stop Region Monitoring 

It is also important to know how to stop monitoring regions. For that, you just need to call ‘stopMonitoringForRegion’: method of ‘CLLocationManager’ object, by passing through the monitored regions. If the specified region is not currently being monitored, this line of code will have no effect.

Only 20 Regions at a Time

The region monitoring feature of iOS only provides the possibility to monitor up to 20 regions at a time. Trying to monitor more than 20 regions will result in an error, indicating that the limit is crossed. If there are more than 20 regions to monitor, then the developer has to handle those geofences himself. It should be ensured that the 20 active monitored regions are those regions closest to the user-location. You can load and unload regions based on the the current location, and you, always, have to choose the closest 20 regions to monitor, which would resolve this limitation to an extent.

How Does it Help the User?

There are many scenarios where geofencing supports practical applications. Parents can be notified with an email when the child’s mobile device is no more in close proximity to the school premises. Tourists can be informed about interesting places if they intend to explore a city or park. Field service technicians can be notified with a message or an alert sound when they are about to reach a customer or when they are in the near vicinity. Practical applications of geofencing are many. These few lines of code (mentioned in the blog) can make life better and easier.

As an iOS developer, do share your views on geofencing and experience building on iOS device platform. You can also refer Location and Maps Programming Guide – iOS Developer Library

By,

Anjana Velayudhan

iOS Developer, RapidValue Solutions

 

ONE COMMENT

  1. Elbert /  October 1, 2014 AT 5:19 am / Reply

    Hey there, are you sure that region call-back methods are still being called when the app is killed?
    I’ve been testing it with my app, but it’s not working when the app is not running.

    Also, are you sure that the indicated 20 regions limitation is per device? Because on my knowledge, it’s 20 per app, not per iDevice.

    Did you managed to create a sample App that would attest this post of yours?

LEAVE A COMMENT

24/7 Toll Free(877)-643-1850(US)

Would you like to know more about us?

  • This field is for validation purposes and should be left unchanged.
Scroll Top