iOS Quick Read: Implementing Background Fetch

Many of the apps that I work on have periodic updates of information, like news, and I thought that these apps would benefit from the new iOS 7 multitasking functionality. My thinking was that if I could update the app while the user is not using the app, their experience upon opening it will be fresh, new content will be available, and the user will feel no need to do a pull to refresh. Then, I realized that if I could predict when the user uses the app, I could schedule the update just before they use it. Well, it turns out iOS 7 introduced new background task handling that helps developers achieve this exact user experience. The functionality is called Background App Fetch.

How does it work? Your App gets launched periodically in the background in order to update its content. The best part is that the update is based on when the user actually uses your application. So, if you have a weather app and iOS figures out that your user checks it every day at 8am before they go to work, then iOS will launch your app in the background at 7:50am so that the content can update before your user even launches the app. Sweet!

Background Fetch is implemented with AppDelegate hooks that I'll describe step-by-step in order to walk you through the three easy steps that I took to build a prototype that implements background fetching. I have provided the sample project which is posted on GitHub that you can use as a reference to follow along step-by-step.

The Step By Step:
1. First, I created an iPhone Single View Application and enabled Background Fetch in Xcode's Project Editor / Capabilities tab under Background Modes (ON): checkoff Background fetch.

2. Next, we'll need to set the minimum background fetch interval since the default is never. This will enable fetches to happen. This is done in the application:didFinishLaunchingWithOptions: method of the AppDelegate.

3. Finally, we'll need to implement a new AppDelegate method when the App is background launched called application:performFetchWithCompletionHandler: It's in this method that we retrieve the new content and then call the completion handler that we are passed.

And that's it! Now you have added the framework to your application to implement background fetches in order to provide a fresh content user experience. You'll need to pass the completionHandler to your specific data fetching methods in your real-world app, and then call the completion handler when you've processed the data and updated your UI.

Testing Background Fetches:
Now, you are probably wondering how do you test this. Xcode has a new item under the Debug menu called Simulate Background Fetch.

Selecting this menu item in Xcode while your app is running in the simulator will close your app in the simulator, thereby sending it to the background, and call the application:performFetchWithCompletionHandler: that we added in the third step. You should see the NSLog we added in the console window.

Where to Next:
If you've enjoyed this article and want to dive deeper into multitasking topics in iOS I recommend David Chan's WWDC 2013 Session 204: What's New With Multitasking (keeping content fresh and interesting). Also, check out Steve Algernon's WWDC 2013 Session 705: What's New in Foundation Networking which is the best session on the new NSURLSession (see what he did there - btw this is Steve's joke - not mine). NSURLSession is the replacement for the decade old NSURLConnection, introduced in iOS 7 and OS X 10.9 Mavericks, that allows out of process background transfers and is the future of networking in Foundation.

In the next article we'll explore Remote Notifications which is another new multitasking API introduced in iOS 7 which allows your app to launch in the background whenever you send it a special type of silent push notification.

Now go off and add background fetching in your killer app.