我在iOS上为背景提取创建了一个测试应用。它通过调试在模拟器上正常工作 - >模拟后台提取。但是在使用iOS 8的设备上,它每天只调用一次。如何强制iOS更频繁地调用它?
以下是我的步骤和代码:
1)项目 - >功能 - >背景模式 - >背景提取
2)AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
}
- (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);
}
每次调用后台提取时都应显示本地通知。
答案 0 :(得分:0)
在这种情况下,您只能验证您的代码是否有效。背景提取发生的确切时间由iOS管理。这已在this SO answer
中讨论过如果您想测试自己的代码,可以选择Xcode->Debug->Simulate Background Fetch