4
votes

I am tired of finding the problem with this but was unable to get why this happens that the Location Manager's delegate method didEnterRegion or didExitRegion is not called ever. I test this in my iPhone but it doesn't work. Please tell me what I am missing. My source code is:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    manager = [[CLLocationManager alloc] init];
    manager.delegate = self;
    dist = 100;
    coord = CLLocationCoordinate2DMake(24.004686, 74.554088);
    region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"];
    [manager startMonitoringForRegion:region desiredAccuracy:10];
    return YES;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    NSLog(@"Enter Region.");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Entered the Location." delegate:nil cancelButtonTitle:@"OK"  otherButtonTitles: nil];
    [alert show];
//    [alert release];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"didExitRegion");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Exited the Location." delegate:nil cancelButtonTitle:@"OK"  otherButtonTitles: nil];
    [alert show];
//    [alert release];
}

Can anybody figure out what's wrong with?? I have searched many demos and source codes but most of them end with didUpdateToLocation, which is called in my case, but I have problem with didEnter and didExit. I have also implemented the didFailWithError but it does not do the thing. Please give any clue why these two methods are not called. or give me any link that calls these methods, I found no examples/source for these methods. Any help will be appreciated.

4

4 Answers

1
votes

is your header file declared as a CLLocationManagerDelegate?

once you setup the region for monitoring you should test by removing that element of the code and then check for the UIApplicationLaunchOptionsLocationKey key when launching like this:

 if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){

        NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
        if(locman == nil){
            locman = [[CLLocationManager alloc]init];


        locman.delegate = self;
        locman.distanceFilter = kCLLocationAccuracyBest;
        }

    }

The location manager delegate will then be called and didEnter and didExit should fire ok.

0
votes

I was able to get didEnterRegion and didExitRegion only after I enabled Background App Refresh in general settings.

0
votes

In viewDidLoad, add the following code

self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];

I hope this will solve your problem.

0
votes

Have you tried didDetermineState as well? didEnterRegion and didExitRegion are only called on enter/exit, but from what I understand if you're in the region and not actually moving, didDetermineState will tell if you if you are currently in the region.