I am using Ibeacon Template example, in that i am using local notifications.when app is not in background “didExitRegion”,“didEnterRegion” and “didRangeBeacons” methods are getting called ramdomly. I don’t have a clear idea on how these methods will work both when in background and killed from background, can anyone please assist me on this.Thanks in advance.
This is sample code i am using:
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[manager stopRangingBeaconsInRegion:(CLBeaconRegion*)region];
[self.locationManager stopUpdatingLocation];
NSLog(@"You exited the region.");
[self sendLocalNotificationWithMessage:@"You exited the region."];
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[manager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
[self.locationManager startUpdatingLocation];
NSLog(@"You entered the region.");
[self sendLocalNotificationWithMessage:@"You entered the region."];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
NSString *message = @"i am in 3 meters.";
IMViewController *viewController = (IMViewController*)self.window.rootViewController;
viewController.beacons = beacons;
[viewController.tableView reloadData];
if(beacons.count > 0) {
CLBeacon *nearestBeacon = beacons.firstObject;
if(nearestBeacon.proximity == self.lastProximity ||
nearestBeacon.proximity == CLProximityUnknown)
{
return;
}
self.lastProximity = nearestBeacon.proximity;
NSLog(@"lastProximity: %ld", (long)self.lastProximity);
NSInteger str=(int)nearestBeacon.accuracy;
//NSString *distance=[NSString stringWithFormat:@"Distance: %d",(int)nearestBeacon.accuracy];
if (str ==3)
{
[self sendLocalNotificationWithMessage:message];
}
}
}