Geofencing in iOS

Description :

This blog introduces you to use geofencing is iOS with in-build method of LocationManager. Geofencing uses global positioning system(GPS) to identify users geographical boundary. Now days geofencing would be too much useful in Internet of Things (IOT) application. As it can gives you position of user and also assigned some boundary with them.

Geofencing is most accurate with WI-FI ON irrespective of it is connected to any network or not.

Implentation :

These are the 2 packages in objective c use to implement geofence :

#import <Foundation/Foundation.h>

#import <CoreLocation/CoreLocation.h>

Which you can included inside LocationManager.

 Now will consider one example to elaborate the implementation of geofence.

Consider user is wanted to go to location A. At this point we can start or define boundary of user with location A.

Geofence_1

Support of this methods are above iOS 7.0 version.

    if(![CLLocationManager locationServicesEnabled])

    {

        //You need to enable Location Services

    }    

    CLLocationDegrees latitude = //Latitude of location A

    CLLocationDegrees longitude =//Longitude of location A

    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

    CLLocationDistance regionRadius = //Radius as per your requirement but as per iOS suggestion it should be above 250-300 meter.

NSString *identifierId = //Any unique value for geofence identifying.

    CLRegion * region = nil;

    if([version floatValue] >= 7.0f

    {

        region =  [[CLCircularRegion alloc] initWithCenter:centerCoordinate                                              radius:regionRadius   identifier:identifierId];

    }

        [locationManagers startMonitoringForRegion:region];    

After start monitoring geofence there is one immediate method called :

– (void)locationManager:(CLLocationManager *)manager  didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region;

This helps you to identify the current state of user with geofence region.

After that there are methods which would help you to monitor geofence region.

– (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;

– (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region:

This 2 method will automatically get trigger once user is inside geofence region didEnterRegion would get called and then once it is outside geofence region didExitRegion would get called.

Pros and Cons :

  • It is very useful in IOT type application or you need to trigger some automatic functions once user reached or leave the field.
  • You can continuously monitor the position of user.
  • It is very easy to implement and introduce in the existing application.
  • As it is using GPS to track user it is battery consuming.

Reference :

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

http://stackoverflow.com/questions/12720658/geofencing-ios-6

Submit a Comment

Your email address will not be published. Required fields are marked *