I set some constant lat and long,Wherever i go it showing inside when i launch my app.stand end exit methods are never called.tried many time getting the same problem.
Here is my code Please help,Thanks in advance
- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeLocationServices];
}
-(void)initializeLocationServices
{
NSLog(@"Started location services");
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_locationManager.pausesLocationUpdatesAutomatically = NO;
[_locationManager startUpdatingLocation]; // to show authorisation popup
}
-(CLCircularRegion*)createRegion
{
// Test coordinates
CLLocationDegrees latitude = 12.908768;
CLLocationDegrees longitude = 77.652061;
CLLocationDistance radius = 50; // meters;
// If radius is too large, registration fails automatically, so limit the radius to the maximum value
if (radius > _locationManager.maximumRegionMonitoringDistance) {
radius = _locationManager.maximumRegionMonitoringDistance;
}
CLCircularRegion* region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(latitude, longitude) radius:radius identifier:@"TEST"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
NSLog(@"Created region");
return region;
}
-(void)monitorProximity
{
CLRegion *region = [self createRegion];
// Check if support is unavailable
if ( ![CLLocationManager isMonitoringAvailableForClass:[CLRegion class]]) {
NSLog( @"Failed to initialise region monitoring: support unavailable");
return;
}
// Check if authorised
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
NSLog( @"Failed to initialise region monitoring: app not authorized to use location services");
return;
} else
{
NSLog(@"Started monitoring proximity");
}
// Clear out any old regions to prevent buildup.
if ([_locationManager.monitoredRegions count] > 0)
{
for (id obj in _locationManager.monitoredRegions)
[_locationManager stopMonitoringForRegion:obj];
}
[_locationManager startMonitoringForRegion:region];
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
NSLog(@"Started monitoring for region: %@", [region description]);
[_locationManager requestStateForRegion:region]; // check if already inside region
}
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"Failed to start monitoring for region: %@", [error localizedDescription]);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed Monitoring" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
NSLog(@"didDetermineState");
if (state == CLRegionStateInside) {
NSLog(@"inside");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"inside" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
return;
} else if (state == CLRegionStateOutside) {
NSLog(@"outside");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"outside" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
} else {
NSLog(@"unknown");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"unknown" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"didEnterRegion");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"did enter region" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"didExitRegion");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"did exit region" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"Monitoring authorisation status is now: %@", status == kCLAuthorizationStatusAuthorized ? @"authorized" : @"not authorized");
if (status == kCLAuthorizationStatusAuthorized) {
[self monitorProximity];
}
}