我知道如果一个应用程序使用“重大变化的位置服务”,如果有一个位置更新要传递,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 = @"Ok";
theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
[[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
}