繁体   English   中英

didEnterRegion永远不会在ios中被调用

[英]didEnterRegion never gets called in ios

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

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

如果您已经在该区域中,则不会收到关于您输入区域的通知,因为状态尚未更改。

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

如果您需要支持iOS 6,则可以通过-[CLRegion containsCoordinate:]检查区域是否包含设备的当前坐标,从而手动执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM