iOS 7 SDK: Background Transfer Service

This post is part of a series called iOS 7 SDK Essentials.
iOS 7 SDK: Working with Background Fetch
iOS 7 SDK: Core Bluetooth - Theoretical Overview

This tutorial will show you how to use the background transfer service, a Multitasking API provided by iOS 7. I'll teach you how to create an app that will download a file without the application in the foreground. Once the file fully downloads, a notification message will pop-up. Continue reading to create this service!


Background transfer service originated with iOS 6. This feature allowed apps to transfer files in both foreground and background modes, but limited the minutes. The biggest problem was when the "limited minutes" did not allow the user to download or upload large files. This is why Apple improved the framework in iOS 7.

With iOS 7, this feature underwent major changes, including:

  • iOS manages downloads and uploads
  • The transfer continues even when the user closes the application
  • Time is unlimited
  • It can be put in the queue anytime (foreground and background)
  • The app wakes up to handle authentication, errors, or completion
  • The app includes a Progress View

Background Transfer Service can be used for several distinct and useful tasks such as: uploading photos or videos, combining background fetch and remote notifications, and for keeping the app up to date, like with purchases for books, TV shows, podcasts, game content, maps, and more.

1. Setup the Project

To create this service, we need a single view with the following properties:

  • A ViewController
  • A NavigationController
  • A Bar Item (to start the Download)
  • An UIDocumentInterationController (to open the PDF document download)

First, start a new Xcode iPhone project. Then create a Single View Application. Next, go to the Main.Storyboard and add some objects to our View. To add the NavigationController select the Default View Controller. In the Xcode menu, select Editor > Embed In > Navigation Controller. You need to drag-and-drop the Bar Item and the Progress View to your View Controller. Once you're finished the View Controller should look similar to the following image:

Figure 1: After Setup - Background Transfer!
Illustration of App - After Setup - Background Transfer

Now, let's add the properties necessary to interact with the objects we added. In ViewController.h, add the following lines:

Now change the view for the ViewController.m. A warning will appear, but don't worry about it; we'll fix it later. Go back to the Main.Storyboard and connect the objects with the properties and actions.

This step is trivial, but if you have any concerns feel free to use the comment section below.

2. NSURLSession

The NSURLSession class and related classes provide an API to download or upload content via HTTP. This API is responsible for managing a set of transfer tasks. You will need to create three objects that directly relate with that class: one NSURLSession, NSURLSessionDownloadTask, and UIDocumentInteractionController.

Your ViewController.h will be something like this:

Additionally, you will also declare four protocols: NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate, and UIDocumentInteractionControllerDelegate. Your @interface should look like:

The properties we'll add are useful to instantiate and manipulate our session and the download process, which will allow you to resume, suspend, cancel, or retrieve the state. The last property, UIDocumentInterationController is used to present the PDF document downloaded in this tutorial.

Now move to ViewController.m.

The first task to complete is to add a string to the location of the file to download. You should use a standard Apple PDF.

On the viewDidLoad method, let's instantiate and set both the session and the progress views:

Since you call the backgroundSession method, and it does not exist, you must declare it now. This method is responsible for dispatching one action that will be the background session. The backgroundSession through the dispatch_once executes a block once for the entire lifetime of the application. The NSString received by the NSURLSessionConfiguration represents the ID of our session. This ID needs to be unique for each NSURLSession instance.

The complete method is as follows:

The (IBAction)start:(id)sender method starts the document download. You will then initiate an NSURL and a NSURLRequest and use the downloadTask property to pass the request object to the downloadTaskWithRequest method. The complete method is below.

3. Protocols

At this point, you'll notice that three warnings are present. They state that the protocol method should be implemented. The NSURLSessionDownloadDelegate protocol defines the methods to handle the download task. To perform the download, it's required to use the three delegate methods.

So, add the following three methods:

  • 1. (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  • 2. (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL {
  • 3. (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes

The (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite method is responsible to track the overall download process. It also updates the progressView accordingly. The complete method is below.

4. Download Task

The (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL method deals with the data itself (origin and destination). It controls the file only once it's completely downloaded. To put it simply, it tells the delegate that a download task has finished downloading. It contains the session task that's finished, the download task that's finished, and a file URL where the temporary file can be found. It should look like this:

Finally, the (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes must also be declared. But be aware that we wont use it any further.

5. Session Tasks

You're almost done with this class, only two methods remain: (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error, and - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session.

The first method informs the delegate that the task has finished transferring data. You should also use it to track any error that occurs.

Finally, you need to add the (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session method. It tells the delegate that all messages enqueued for a session have been delivered. It instantiates your AppDelegate in order to launch a UILocalNotification. The method you need to use is:

Several errors will appear because you haven't imported the AppDelegate.h into your class yet.

Move to the AppDelegate.h and add the two following objects:

In the AppDelegate.m you should implement the delegate method (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler. It tells the delegate that events related to a URL session are waiting to be processed and calls a custom method (presentNotification) to notify the user when the file completely downloads. The complete method is:

6. Local Notification

The presentNotification method uses the UILocalNotification class to create a local notification. It creates a sound and uses the badge system for that notification. Here's the complete method:

By the way, to restart the counter on the top left at the App Icon, you must add the following line to the (void)applicationDidBecomeActive:(UIApplication *)application method in the AppDelegate.

The notification should be similar to the following image:

Figure 2: Notification!
Illustration of App - Notification - MTBackgroundTransfer

It is now time to Run the app and test the background download.


At the end of this tutorial, you should have completed your background transfer service in iOS 7. You should understand the background transfer service and how to implement it. If you have any questions, please leave them in the comment section below.