我有一个应用程序,即使在后台或终止(被杀掉),也必须报告用户位置。问题在于,该应用程序应该不少于每小时报告一次位置。
我正在使用重大位置更改(SLC)来跟踪所有移动,当用户在行进时这已经足够了,但是一旦用户停止,就不会再更新,并且应用程序没有机会提交新位置(当用户停留在同一区域但没有SLC时)。
为了解决这个问题,我开始使用后台获取以定期发送后台更新位置(即使没有SLC)。问题在于,后台获取之前经常工作(自从我在另一个iOS 7.x应用程序中使用它以来,每20-30分钟),但现在在iOS8 / iOS9上,我只能得到一天左右的时间,这在我的情况下是不可接受的。我执行了大量测试,开发了一个简单的后台获取应用程序,在获取时添加本地通知。无法强制其更频繁地工作。
以下是我的AppDelegate:
我正在使用重大位置更改(SLC)来跟踪所有移动,当用户在行进时这已经足够了,但是一旦用户停止,就不会再更新,并且应用程序没有机会提交新位置(当用户停留在同一区域但没有SLC时)。
为了解决这个问题,我开始使用后台获取以定期发送后台更新位置(即使没有SLC)。问题在于,后台获取之前经常工作(自从我在另一个iOS 7.x应用程序中使用它以来,每20-30分钟),但现在在iOS8 / iOS9上,我只能得到一天左右的时间,这在我的情况下是不可接受的。我执行了大量测试,开发了一个简单的后台获取应用程序,在获取时添加本地通知。无法强制其更频繁地工作。
以下是我的AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
return YES;
}
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
localNotification.fireDate = now;
localNotification.alertBody = [NSString stringWithFormat:@"Background fetch!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
NSInteger number = [UIApplication sharedApplication].applicationIconBadgeNumber;
number++;
localNotification.applicationIconBadgeNumber = number;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
completionHandler(UIBackgroundFetchResultNewData);
}
这里所做的一切就是在每个后台抓取中添加本地通知。我总是用UIBackgroundFetchResultNewData来最终执行后台操作。
你有关于如何强制后台抓取更频繁工作(或证明现在不再可能)的建议吗?欢迎任何满足我的要求的替代方案!