CLLocationManager仅在安装后第一次运行应用程序时监视区域

我有一个iOS应用程序,它是一个标签式应用程序,有3个视图控制器,所有这些都需要知道手机什么时候进入特定的地理区域。

我们监视的区域是在运行时通过Web界面提供的,因此我们需要定期清除CLLocationManager正在监视的区域并添加新区域。 CLLocationManager对象是singleton类的成员变量,它也管理与Web服务器的连接。

我遇到的问题是,首次安装应用程序时,区域监控工作正常。 但是我第一次尝试在第一次运行它之后 ,区域监控不起作用。

我可以在实际手机 iOS模拟器上看到这一点。

收到包含区域详细信息的服务器的mesasge后,我们运行以下代码:

-(void) initialiseLocationManager:(NSArray*)geofences { if(![CLLocationManager locationServicesEnabled]) { NSLog(@"Error - Location services not enabled"); return; } if(self.locationManager == nil) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; } else { [self.locationManager stopUpdatingLocation]; } for(CLRegion *geofence in self.locationManager.monitoredRegions) { //Remove old geogate data [self.locationManager stopMonitoringForRegion:geofence]; } NSLog(@"Number of regions after cleanup of old regions: %d", self.locationManager.monitoredRegions.count); if(self.locationManager == nil) { [NSException raise:@"Location manager not initialised" format:@"You must intitialise the location manager first."]; } if(![CLLocationManager regionMonitoringAvailable]) { NSLog(@"This application requires region monitoring features which are unavailable on this device"); return; } for(CLRegion *geofence in geofences) { //Add new geogate data [self.locationManager startMonitoringForRegion:geofence]; NSLog(@"Number of regions during addition of new regions: %d", self.locationManager.monitoredRegions.count); } NSLog(@"Number of regions at end of initialiseRegionMonitoring function: %d", self.locationManager.monitoredRegions.count); [locationManager startUpdatingLocation]; } 

我试过在各个地方调用[locationmanager stopUpdatingLocation],特别是在AppDelegate.m文件中的各个地方(applicationWilLResignActive,applicationDidEnterBackground,applicationWillTerminate),但它们似乎都没有帮助。 无论哪种方式,当我构建我的应用程序并添加GPX文件来模拟位置时,模拟器正确地拾取Web界面发送的区域。 第二次运行程序时,区域没有被选中。 当我重新加载GPX文件时,它再次起作用,但从第二次开始,它再也无法工作了。

根据API文档,CLLocationManager甚至在终止时保留区域记录(这就是我清除我们监视的区域的原因),但我的猜测是我的初始化例程在应用程序第一次运行时是好的,但是调用第二次不应该调用的东西。 此外,清除过程似乎并不总是有效(NSLog语句通常显示CLLocationManager清除为0个区域,但并非总是如此)。

任何想法为什么这不起作用?

所以让我们清理一下

使用区域监视时,您不需要调用startUpdatingLocationstopUpdatingLocation 。 那些激活标准位置跟踪发送消息到委托回调locationManager:didUpdateLocations: . 区域跟踪应用程序实现这些委托回调:

 – locationManager:didEnterRegion: – locationManager:didExitRegion: 

此方法也不太可能在此方法中禁用位置服务,您应确保无法从后台线程中删除“locationManager”。 因此,我们不需要检查它们两次。

由于您正在监控区域,因此请确保使用+ (BOOL)regionMonitoringAvailable进行区域监控。 最好还是在某些时候使用+ (CLAuthorizationStatus)authorizationStatus检查位置权限并做出适当的反应。

根据这篇博文 ,你似乎也需要

 UIBackgroundModes :{location} UIRequiredDeviceCapabilities: {location-services} 

在您的应用程序info.plist中,这一切都正常工作。

如果你有关于失败模式的更多信息,我可以回来一些更具体的建议:)