didEnterRegion永远不会在ios中被调用 [英] didEnterRegion never gets called in ios
问题描述
不确定我在做什么错。我希望触发本地通知,告诉用户他在特定物体附近。我的代码如下:
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];
}
推荐答案
如果您已经在区域,因为状态未更改,您不会收到通知。
If you are already in the region, you will not get notified that you entered a region, because the state hasn't changed.
在iOS 7及更高版本上,您可以使用-[CLLocationManager requestStateForRegion:]
在开始监视区域时请求区域的当前状态。在您的CLLocationManagerDelegate上-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
。
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.
如果需要支持iOS 6,则可以通过-[CLRegion containsCoordinate:]
来检查区域是否包含设备的当前坐标,从而手动完成此操作。
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:]
这篇关于didEnterRegion永远不会在ios中被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!