Skip to content

Instantly share code, notes, and snippets.

@mombrea
Created February 4, 2014 22:02
Show Gist options
  • Save mombrea/8813243 to your computer and use it in GitHub Desktop.
Save mombrea/8813243 to your computer and use it in GitHub Desktop.
Example of enabling background app refresh and updating the app icon badge number
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if ([[ItemManager new] reloadItemDataSynchronous]) {
ItemHelper *helper = [ItemHelper new];
NSArray *items = [helper getNewItems];
if(items.count > 0){
int newItemCount = [helper getNewItemCount];
[UIApplication sharedApplication].applicationIconBadgeNumber = newItemCount;
}
}
completionHandler(UIBackgroundFetchResultNewData);
}
@rsimenok
Copy link

rsimenok commented Dec 4, 2017

Looks like this is not working on iOS 11, and some lower versions i think.

@puppybits
Copy link

These type of background tasks just active whenever iOS feels like it. For most apps I've done you're lucky if you get called twice a day.

iOS 11 has a new API that lets you schedule real background network operations. https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background?language=objc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment