Reputation: 95
Not sure what I'm doing wrong. I want a local notification to be triggered, telling the user that he's near by a particular object. My code is as following:
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
CLLocationDistance radius = 1000.00;
CLLocationCoordinate2D location2D = CLLocationCoordinate2DMake(_location.coordinate.latitude, _location.coordinate.longitude);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:location2D
radius:radius
identifier:@"theRegion"];
[self.locationManager startMonitoringForRegion:region];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"GeoFence: didEnterRegion");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"You entered a region";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
Upvotes: 1
Views: 1122
Reputation: 2525
If you are already in the region, you will not get notified that you entered a region, because the state hasn't changed.
On iOS 7 and later, you can use -[CLLocationManager requestStateForRegion:]
to request the current state for a region as you start to monitor it. -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
on your CLLocationManagerDelegate.
If you need to support iOS 6, you can do this manually by checking if the region contains the device's current coordinate, via -[CLRegion containsCoordinate:]
Upvotes: 2