ios - 位置管理器 :didExitRegion: not called when screen is off

标签 ios iphone objective-c core-location geofencing

我正在使用区域监控在用户退出区域时运行一些代码。当应用程序在前台或后台运行且设备屏幕位于 locationManger:didExitRegion: 时,将按预期调用。但是,当屏幕关闭时,我通常不会收到回调,直到我通过单击电源按钮打开屏幕,此时立即调用 locationManger:didExitRegion:。所有测试均在 iPhone 5s 上完成。

以下是我设置区域的方式:

self.monitoredRegion = [self setupGeoFenceWithCenter:self.currentLocation radius:200];
[self.locationManager startMonitoringForRegion:self.monitoredRegion];

-(CLRegion *)setupGeoFenceWithCenter:(CLLocation *)center radius:(CGFloat)radius
{
    if (radius > self.locationManager.maximumRegionMonitoringDistance)
    {
        radius = self.locationManager.maximumRegionMonitoringDistance;
    } 
    CLRegion * region =  [[CLCircularRegion alloc] initWithCenter:center.coordinate
                                                           radius:radius
                                                       identifier:@"geofence"];
    return region;
}

这是委托(delegate)回调:

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"Exited Region");
}

这是 CLLocationManager 初始化:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = kCLDistanceFilterNone;

此外,最近我注意到 locationManager:monitoringDidFailForRegion:withError: 方法被更频繁地调用,并出现以下错误描述:

Error Domain=kCLErrorDomain Code=5 "The operation couldn’t be completed. (kCLErrorDomain error 5.)"

但即使出现此错误,当应用程序位于前台时,它仍会按预期工作。 这是区域监控的预期行为吗?有人遇到过这种情况么?这可能是我收到的错误的结果吗?

谢谢!

最佳答案

您是否尝试等待几分钟(4 到 15 分钟)? PS:在关闭 iDevice 之前,您的应用程序应该处于前台。这是基于 iBeacon 测试...希望它对您的情况有所帮助。

关于ios - 位置管理器 :didExitRegion: not called when screen is off,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22126866/

相关文章:

ios - RxDataSources 和改变模型的能力

ios - 提取请求,该请求返回coredata URL。为什么?以及如何访问托管对象?

objective-c - 在后台接收蓝牙管理器通知

ios - AVAudioSession setCategory 不工作

ios - 获取 UICollectionViewCell 的 indexPath

iphone - Alpha 透明 PNG 在 Mobile Safari 中无法正确显示

iphone - 检查配置文件的开发人员证书有效性

iphone - 当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算 UILabel 的字体大小?

objective-c - 如何在 NSOutlineView 中制作带有渐变背景的标题行?

ios - 遍历数组,索引错误