2

I created a test app for background fetches on iOS. And it works fine on the simulator via Debug -> Simulate Background fetch. But on device with iOS 8 it called only once a day. How to force iOS to call it more often?

Here are my steps and code:

1) Project->Capabilities->Background modes->Background fetch

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

Local notification should appear every time background fetch invoked.

Nadzeya
  • 641
  • 6
  • 16
  • 1
    Never called or called once a day, which is it? Your two statements are in total contradiction to each other. You can't force when it will occur, the OS decides when it will be, and if the OS is invoking it once a day then its working. When and how often it gets called depends on app device usage. – Gruntcakes Sep 22 '15 at 15:14
  • How often do you want it background fetch to be called? – Black Frog Sep 22 '15 at 15:19
  • possible duplicate of [performFetchWithCompletionHandler not getting called on fixed time interval](http://stackoverflow.com/questions/25161168/performfetchwithcompletionhandler-not-getting-called-on-fixed-time-interval) – ChrisH Sep 22 '15 at 15:20
  • @MartinH sorry for such contradiction, I mean that it calls only once and never calls more – Nadzeya Sep 22 '15 at 16:06
  • @BlackFrog at least second time :) – Nadzeya Sep 22 '15 at 16:08

1 Answers1

0

All you can do in this scenario is verify that your code works. The exact timing of when the background fetch happens is managed by iOS. This has been discussed in this SO answer

If you want to test your own code, you can do so with your app running in the Simulator by choosing Xcode->Debug->Simulate Background Fetch

Community
  • 1
  • 1
ChrisH
  • 4,468
  • 2
  • 33
  • 42