About the behaviour of ibeacons when app is in background or killed from background?

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];
        }    
    }
}

didRangeBeacons works only when the app is running—either on screen, or woken up temporarily into the background by enter or exit event.

When you leave the app (e.g., by pressing the home button, or locking the screen), it doesn’t actually go to the background, it is being suspended completely, and no code can be executed.

But if you started monitoring, and then an enter or exit event happens, iOS will wake your app into the background for about 10 seconds, and during that time you can execute some code (like, show a notification), or do ranging.

If an app was killed in the meantime (either swiped away from the app switcher, or killed by iOS to free up some memory), iOS will launch it anew, but it won’t instantiate any View Controllers, so you must be careful to place your code in the App Delegate.

It’s all described in our iBeacon tutorial, so once again I encourage you to go through it:
http://developer.estimote.com/ibeacon/tutorial/part-2-background-monitoring/

And you can use the Notification template to see the code, and how it should be implemented:
https://cloud.estimote.com/#/apps/add/notification