我正在寻找一种在iOS应用程序中每n分钟更新一次后台位置的方法.我正在使用iOS 4.3,该解决方案适用于非越狱的iPhone.
我尝试/考虑了以下选项:
CLLocationManager startUpdatingLocation/startMonitoringSignificantLocationChanges
:这根据预期在后台工作,基于配置的属性,但似乎不可能强制它每n分钟更新一次位置NSTimer
:当应用程序在前台运行但似乎不是为后台任务设计时,是否有效UIApplication:beginBackgroundTaskWithExpirationHandler
:据我所知,当应用程序移动到后台而不是实现"长时间运行"的后台进程时,这应该用于在后台完成一些工作(也限制时间).如何实施这些常规后台位置更新?
我正在编写一个需要高精度和低频率的背景位置更新的应用程序.该解决方案似乎是一个后台NSTimer任务,它启动位置管理器的更新,然后立即关闭.之前已经问过这个问题:
但我还没有得到最起码的例子.在尝试了上述接受的答案的每一个排列后,我总结了一个起点.输入背景:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"ending background task");
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
self.timer = [NSTimer scheduledTimerWithTimeInterval:60
target:self.locationManager
selector:@selector(startUpdatingLocation)
userInfo:nil
repeats:YES];
}
Run Code Online (Sandbox Code Playgroud)
和委托方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", newLocation);
NSLog(@"background time: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
[self.locationManager stopUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)
当前行为是backgroundTimeRemaining
从180秒减少到零(记录位置时),然后执行到期处理程序,不再生成进一步的位置更新.如何修改上述代码以便在后台无限期地接收定期位置更新?
更新:我的目标是iOS 7,似乎有一些证据表明后台任务的行为有所不同:
objective-c core-location ios background-task ios-background-mode
我最近升级了我的iOS设备以使用iOS 7.我们正在开发的其中一个应用程序使用后台位置服务来跟踪设备位置,我们所有的测试人员都报告该应用程序不再出现在iOS下的后台跟踪7.
我们已经确认在设备上的设置中启用了应用程序的后台处理,并且之前的版本在iOS 6下运行良好.即使设备已循环,应用程序也会在位置更新后重新启动.
还有什么需要做才能在iOS 7下完成这项工作吗?
我的应用程序在后台运行的iOS 5
和iOS 6
,但在iOS的7它是创造问题.
当我在控制台应用程序中看到继续在后台执行任务时,但是当我点击应用程序进入后台时,应用程序将进入第一个应用程序屏幕(当我们启动应用程序时).在控制台中,它不会告诉应用程序崩溃或类似的东西.它甚至不会打印写入的控制台消息applicationWillEnterForeground
和applicationDidBecomeActive
方法.我没有得到这个问题.我正在构建 iOS6
并安装应用程序iOS7
.请帮忙.