Background Fetch is one of the most impressive features released with iOS 7. It basically works in the same lines of a default traffic application. I am here to explain how to make your app work in the background using Background Fetch. Before we begin with the actual tutorial, let me walk you through the various background modes available for iOS apps.
Audio: The app has the ability to record and/or play audio in background.
Location Updates: Getting device locations while the app is in background is another feature.
VOIP (Voice Over Internet Protocol): The app can make video calls over Internet connection.
Newsstand Updates: These are specific to Newsstand apps that download and processes magazine or newspaper content in the background.
Background fetch: The app regularly downloads and processes small amounts of content from the network.
Apart from the above modes there is External accessory communication, Bluetooth accessory communication, which any app should support in order to function in the background.
In addition, every app has some extra time that iOS offers to resolve certain unfinished business before the app goes into suspended mode. For iOS 7 the time has been reduced to around 180 secs (3 mins); earlier it was around 600 secs (10 minutes).
To earn extra time we need to initialize UIBackgroundTaskIdentifier
-(void) beginBackgroundUploadTask { if(self.backgroundTask != UIBackgroundTaskInvalid) { [self endBackgroundUploadTask]; } self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self endBackgroundUploadTask]; }]; }
Once the background task runs out of time from the stipulated allotted time, we should invalidate and end the background task.
-(void) endBackgroundUploadTask { [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask ]; self. backgroundTask = UIBackgroundTaskInvalid; }
Background Fetch:
Now let’s see how to support Background Fetch in any iOS app. Please follow the below mentioned steps.
1. Enable Background Modes in Xcode → Select Project File → Capabilities → Check the Background fetch checkbox.
2. After you enable Background fetch, the background fetch interval must be set. It is by default set to UIApplicationBackgroundFetchIntervalNever; we need to set it to UIApplicationBackgroundFetchIntervalMinimum
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Set Minimum Background Fetch Inteval // if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum]; } return YES; }
3. Once background fetch is properly configured, the applications performFetchWithCompletionHandler delegate method is called for each fetch.
Please write down the below mentioned method in your AppDelegate.m file.
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { completionHandler(UIBackgroundFetchResultNewData); }
This method is called whenever background fetch is triggered. We need to specify the completionHandler with following parameters: For success UIBackgroundFetchResultNewData, for failure UIBackgroundFetchResultFailed and in case of no change in the data contents UIBackgroundFetchResultNoData
4. You can simulate background fetch on iOS Simulator, Xcode → Debug → Simulate Background Fetch.
5. To Enable Background Fetch on iOS Simulator Go to Xcode → Product → Scheme → Edit Scheme → Options → Check the Enable Background fetch checkbox.
I have developed a sample app that fetches temperature every 10 secs from the OpenWeather.com. You can also manually simulate background fetch by following Step 5.
Download Source code here.