didEnterRegion永远不会在ios中被调用

时间:2014-04-29 20:50:20

标签: ios notifications core-location

不确定我做错了什么。我希望触发本地通知,告诉用户他靠近特定对象。我的代码如下:

- (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 个答案:

答案 0 :(得分:2)

如果您已经在该地区,则不会收到您进入某个地区的通知,因为状态未发生变化。

在iOS 7及更高版本中,您可以使用-[CLLocationManager requestStateForRegion:]在开始监控区域时请求区域的当前状态。 CLLocationManagerDelegate上的-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region

如果您需要支持iOS 6,可以通过-[CLRegion containsCoordinate:]

检查区域是否包含设备的当前坐标来手动执行此操作