我正在编写一个需要高精度和低频率的背景位置更新的应用程序.该解决方案似乎是一个后台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也会在有位置更新时将其唤醒.
如果应用程序使用标准位置服务并指定位置作为UIBackgroundModes的密钥,我无法找到关于此案例的明确答案:iOS是否也会将其唤醒以提供更新,即使它已终止?或者应用程序是否需要在后台运行以获取位置更新回调?
更新:当我问这个时,我没有时间去测试它.但是在我得到答案后,我在我的应用程序的委托中编写了这段代码,看看我的终止应用程序是否会在获得位置更新时重新启动.当我收到更新通知时,我正在显示UILocalNotification.但是,当我终止我的应用程序,然后更改我在城市的位置,该应用程序没有重新启动,我没有得到任何更新.你能告诉我我做错了什么吗?
更新#2:根据此问答中的最终结果,此代码没有任何问题,这是使用标准位置服务的应用程序的预期行为,而不是在终止后重新启动.
我已将位置添加为Info.plist文件中的UIBackgroundModes之一.
这是我的应用代表的位置相关部分:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
m_locManager = [[CLLocationManager alloc] init];
m_locManager.delegate = self;
[m_locManager startUpdatingLocation];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[m_locManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog("%@", [NSString stringWithFormat:@"Background Fail %@", [error localizedDescription]]);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
UILocalNotification * theNotification = [[UILocalNotification alloc] init];
theNotification.alertBody = [NSString stringWithFormat:@"Background location %.06f %.06f %@" , newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp];
theNotification.alertAction …Run Code Online (Sandbox Code Playgroud) 我是iPhone编程的新手.我正在寻找关于如何使用Manager的startMonitoringSignificantLocationChanges方法的示例或演示CoreLocation.
我对文档感到困惑,因为我不确定是否在调用的情况下调用相同的委托方法startUpdatingLocation.(即代表locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation: (CLLocation *)oldLocation被召唤)
任何有关这方面的帮助将不胜感激.另外,我正在通过子类化在模拟器上测试它CLLocationManager.此子类在iPhone模拟器上运行时提供位置服务的模拟.有没有更好的方法来测试这个.