当app在后台时,CLLocationManager有时会停止更新

bee*_*eev 4 iphone cllocationmanager ipad cllocation ios

我有一个应用程序,用于CLLocationManager跟踪用户的路线,沿着所采取的路径绘制点.该应用程序在后台使用必需的后台模式>应用程序寄存器进行位置更新.

据我所知,后台发生的任何事情都需要调用,locationManager:didUpdateToLocation:fromLocation因为这是每个位置更新调用的方法.

我遇到的问题是有时会停止调用.当用户的位置在大约15分钟左右的时间内没有太大变化时,似乎会发生这种情况.据我所知,调用locationManager:didUpdateToLocation:fromLocation只是停止,大概是为了节省电池.不幸的是,当你回到移动状态时,它不会再次恢复.

我认为无法覆盖此行为,因此我想使用通知中心通知用户应用程序不再记录路由.问题是,应用程序如何知道发生了这种情况?如果未调用locationManager:didUpdateToLocation:fromLocation,则无法触发我的通知.如果正在调用它,则不应触发通知.

是否有某种系统通知表明位置更新将停止?

我发现很难调试这个,因为当我出去测试设备上的位置时,我无法随身携带我的Mac(在模拟器中你只能做很多事情).任何调试技巧也将非常感谢!

Ron*_*Ron 7

如果你还没有找到答案,我认为是因为添加了一个新的属性来CLLocationManager调用pausesLocationUpdatesAutomatically.属性默认为YES,其行为与您描述的完全一致.尝试将其设置为NO,我认为它将解决您的问题.


Eri*_*ric 1

有一个位置更新委托失败了

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)

有几种错误:kCLErrorDenied kCLErrorNetwork在此处添加代码以在上面的委托方法中处理它们而不更新位置,也许是UIAlertView告诉用户。

就我个人而言,我会调用 [locationManager stopUpdatingLocation];任何错误,然后根据失败的原因重新启动它并显示错误消息。

另外,请检查您的 appDelegate 中的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [self saveContext];
    if ([CLLocationManager significantLocationChangeMonitoringAvailable]) {
        // Stop normal location updates and start significant location change updates for battery efficiency.
        [self.locationHandler.locationManager stopUpdatingLocation];
        [self.locationHandler.locationManager startMonitoringSignificantLocationChanges];
    }
    else {
        NSLog(@"Significant location change monitoring is not available.");
    }
}
Run Code Online (Sandbox Code Playgroud)

最后回复:测试。您可以通过更改模拟器中的位置移动来模拟一些位置错误。例如,从跑步到驾驶会导致错误。从运行转到单个特定的自定义位置将导致错误。它们应该全部出现在上面 locationManager 的委托方法中。