Get Started with Geofencing on iOS Device

iOS provides its great core location services. To explore these services one need to use the core location framework. There are lots of apps increasing day by day that are based on location services. You can create an iOS application that provides all the information regarding the region and all the surrounding objects that exist around the user. You can create and monitor the regions as per your requirement. Here monitoring means, your app keeps checking that whether you have been in or out from any geofenced regions.

Geofencing is basically used to monitor the regions or the locations from your app, iOS provide some features using which you can monitor the regions. For example you want to monitor that in which region you are entering and in which region you are exiting. This monitoring feature of this app can manage all these.

To create and implement this type of iOS application, you should have started the location services on your device. Some services are supported in some versions only so; you should care about this while developing any app regarding this. To get the location details, you should configure the core location object and should be easily get the location through Wi-Fi network.

Once you configure the location manager and implement the delegate in your class, you must implement the delegate methods in order to get the surrounding information and location information in your app. Before starting the iOS applications development, you need to have some regions data like coordinate, which represents the region, radius of that region in which you want to monitor and a unique name, which can be used to identify the regions uniquely. You can store those data into a plist file or whatever suitable to you.

Configure your app for location services:

First of all you need to check the location services whether services are enabled or not. If services are disabled, you need to show proper message to the user in order to start the location services. The below code will check the location services are enabled or not.

if(![CLLocationManager locationServicesEnabled])
        NSLog(@"Location services are disabled. Please start the location services.");
        locationManager  = [[CLLocationManager alloc]init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        [locationManager startUpdatingLocation];

If location services are disabled, you need to make it enable in order to work fine for this iOS application. Once location services are enabled, you can start monitoring the regions. The below code described that how to monitor the regions.

for(int i=0;i<locations.count;i++)
        CLLocation *location = [locations objectAtIndex:i];
        CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) radius:100.0 identifier: @"Your Identifer"];
        [locationManager startMonitoringForRegion:region];

Here you can also specify the region identifier also. So that you can easily identify the monitoring regions. Once you have started the regions under the monitoring of location manager, it will automatically keep monitoring the regions. As there is any change in the location or the region of your or user’s region, the location manager will automatically call the delegate methods, so that you can notify the user about the regions changes or location change for the specified region.

Location manager will call the following method when any region is put under the monitoring.

-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
    [self showAlert:[NSString stringWithFormat:@"Started Monitoring region : %@ - Region",region.identifier]];

Once the monitoring is started, it will give the location change updates in the following method.

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    [self showAlert:[NSString stringWithFormat:@"You are entering : %@ - Region",region.identifier]];

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    [self showAlert:[NSString stringWithFormat:@"You are exiting : %@ - Region",region.identifier]];

The two methods described above called when user changes his/her region. First method didEnterRegion: will be called when user is entering in any region, which is under monitoring. And second method will be called when user is exiting from any region, which is under monitoring.


Post a Comment

Mobile App Development © 2012 | Designed by SPEC INDIA