didEnterRegion never gets called in ios

隐身守侯 提交于 2019-12-08 08:52:17

问题


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

回答1:


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:]



来源:https://stackoverflow.com/questions/23374978/didenterregion-never-gets-called-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!