2

我为 iOS 上的后台提取创建了一个测试应用程序。它通过调试在模拟器上正常工作 - >模拟后台获取。但在装有 iOS 8 的设备上,它每天只调用一次。如何强制 iOS 更频繁地调用它?

这是我的步骤和代码:

1)项目->功能->后台模式->后台获取

2) 应用代理:

- (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);
}

每次调用后台提取时都应出现本地通知。

4

1 回答 1

0

在这种情况下,您所能做的就是验证您的代码是否有效。后台获取发生的确切时间由 iOS 管理。这已在this SO answer中讨论过

如果您想测试自己的代码,您可以在模拟器中运行您的应用程序,方法是选择Xcode->Debug->Simulate Background Fetch

于 2015-09-22T15:17:17.480 回答