Some of the apps I work on have periodic updates of information like news that I thought would benefit from new iOS 7 multitasking functionality. If I could update the app, I thought, while the user is not using the app then when the user opens the app the news will be fresh, new content will be available, and the user will feel no need to do a pull to refresh. Better yet if somehow I could predict when the user uses the app I could schedule the update just before they use it. Well, 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 you may be thinking. Your App gets launched periodically in the background in order to update its content. The best part is that its based on when your 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 the content can get updated 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. I first 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. We’ll next need to set the minimum background fetch interval since the default is never. This enable these fetches to happen. This is done in application:didFinishLaunchingWithOptions: method.
3. Finally we’ll need to implement a new AppDelegate method that is called when the App is background launched called application:performFetchWithCompletionHandler: Its 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 implements 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 in Xcode will background your app 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 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) and 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 iOS7 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.
One thought on “Implementing Background Fetch”
Pingback: Implementing Remote Notifications | [[Cocoa alloc] init];