Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger an event when the user exits a geofence

Is there a way to define a geofence (center and radius) around the current location of an iOS device, and have the system trigger a callback function in my app when the device exits the geofence? Will this mechanism can wake up a closed app?

I would like to avoid extensive GPS usage, so I would prefer a system message over periodic GPS polling, even at the price of reduced accuracy.

like image 554
Adam Matan Avatar asked Mar 20 '23 23:03

Adam Matan


2 Answers

Your solution is Region Monitoring.

In iOS, regions associated with your app are tracked at all times, including when your app is not running. If a region boundary is crossed while an app is not running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it is woken up and given a short amount of time (around 10 seconds) to handle the event.

Whenever an app request for region monitoring, the iOS takes the stand then. Your app registers some location and asks iOS to monitor the region & notify as on entering or exiting the region with precise accuracy.

Like CLRegion *region = [[CLCircularRegion alloc] initWithCenter:[location coordinate] radius:250.0 identifier:[[NSUUID UUID] UUIDString]];

Now iOS takes this request & add it to the system queues of region monitoring with an internal identification to your app. As soon as the device enters the region or exits the region, the iOS send a notification to app to rise up & fire the delegate.

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

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

Please note, if your app is running in the background, the iOS will make your app start in background, once the device enters / exists the registered region.

It is one of the key point on how FourSquare & other similar apps trie to perform much of location data collection & sending it to server & giving user a Tailored message within a small amount of time.

like image 148
Balram Tiwari Avatar answered Apr 02 '23 12:04

Balram Tiwari


Apple documentation for Region Monitoring

Perfect tutorial which teaches you to build a geo fence step by step in ios

Following are the delegate methods which are triggered when a user enters and exits a region!

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Welcome to %@", region.identifier);
}


-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"Bye bye");
}

-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(@"Now monitoring for %@", region.identifier);
}
like image 29
Harsh Avatar answered Apr 02 '23 10:04

Harsh