NSURLSession – Out of process upload/download

ref – http://www.techotopia.com/index.php/An_iOS_7_Background_Transfer_Service_Tutorial

demo project

Background Uploads and Downloads

Adding support for background uploads and downloads is surprisingly easy with NSURLSession. Apple refers to them as out-of-process uploads and downloads as the tasks are managed by a background daemon, not your application. Even if your application crashes during an upload or download task, the task continues in the background.

Enabling background uploads and downloads is nothing more than flipping a switch in your session’s configuration. With a properly configured session object, you are ready to schedule upload and download tasks in the background.

When an upload or download is initiated:

  1. background daemon comes into existence
  2. The daemon takes care of the task and sends updates to the application through the delegate protocols declared in the NSURLSession API
  3. If app stops, daemon continues task
  4. If download task finishes, daemon informs session.
  5. The session then invokes the appropriate delegate methods to make sure your application can take the appropriate actions, such as moving the file to a more permanent location.

When the Background Transfer Service gets in action, what is actually happening is that the operating system takes charge of all the download process, performing everything in background threads (daemons).

While a download is in progress, delegates are used to inform the app for the progress, and wakes it up in the background to get more data if needed, such as credentials for logging in to a service. However, even though everything is controlled by the system, users can cancel all downloads at any time through the application.

A reminder that a block parameter has 3 parts. The return, the parameter, and the block name. Like so:

So this means that the block is used where it takes 2 doubles, and does not return anything. It is called by using the block name ‘block’.

Handle Events for Background Session

Apps that (using an NSURLSession with a background configuration) may be launched or resumed in the background in order to handle the
completion of tasks in that session, or to handle authentication.

In other words, when an app uses NSURLSession with background service (whether its a GET request or downloading/uploading), NSURLSession needs to let the app know and to handle the completion of tasks for that session.

We simply strong the completionHandler block in order to call it later to let the system know that we complete all tasks.

NSURLSessionDelegate’s PROTOCOL METHOD will be called. In there you access the appDelegate and call the completionHandler.

Hence when you’re using it, if an application has received an -application:handleEventsForBackgroundURLSession:completionHandler:
message, the session delegate will receive this message to indicate
that all messages previously enqueued for this session have been
delivered.

Declare the session completion handler member variable:

AppDelegate.h

Then, have its set property copy the completion handler:

AppDelegate.m

Go to your ViewController and conform to NSURLSessionDelegate.

This is so that we need to implement URLSessionDidFinishEventsForBackgroundURLSession.

Now when your session object’s delegate queue tasks have all been processed, in your AppDelegate, you will receive a
application has received an -application:handleEventsForBackgroundURLSession:completionHandler:
message and your protocol method

will be called.

Then your session delegate will send a message to protocol method URLSessionDidFinishEventsForBackgroundURLSession to indicate that all messages previously enqueued for this session have been
delivered.

Hence that’s why its the job of your URLSessionDidFinishEventsForBackgroundURLSession method to simply call the completionHandler and clean it up.

If your session delegate sends a message to call protocol method:

for authentication purposes, then you need to something similar. Where you give authentication information, then call the completionHandler().

Prepare the UI

Configuration Object

Session Object

Updating download progress percentages

When you resume a download task

Send last message related to a specific task

When your download have completed