iOS7 NSURLSession

NSURLSession is iOS7 networking API, which would replace the NSURLConnection method. The new NSURL Session would be the preferred method for the networking. NSURLSession is both class and both suite of classes, it provides the new new tools to upload, downloads and authorizations.

NSURLSession has many benefits some of them listed below.

Enables Background Uploads and Downloads
Background upload and download is one of the features of the iOS7, NSURLSession provide benefits to Upload and Download in the background.

Control networking operations
The NSURLSession class provide the control over the networking operations, it can halt and restart the networking operations.

Put requests into the configurable containers
Each NSURLSession has the configurable containers, for example headers are need to configure only once, and all the request of the session would use the same configuration.

Authentication Handling
NSURLSession class handles the authentication connection basis, it is possible to identify the requests for authentication.



There are three types of NSURLSession cofiguration.

Default Session Configuration:Default configuration creates objects which creates global cache, cookies and storage objects.

Ephemeral Session Configuration: Ephermal configuration is used for private configuration and this configuration doesnot have global cache, cookies and storage objects.

Background Session Configuration: This configuration is used to make the networking calls.


Create NSURLSession

NSURL session create tasks using block based convenience methods or by setting up delegate or via both. For example to download image it require NSURLSessionDownloadTask, first it needs to create a session, below example shows on how to create a session.

NSString *imageUrl =


NSURLSessionConfiguration *sessionConfig =

[NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session =

[NSURLSession sessionWithConfiguration:sessionConfig




After creating a session, image can be downloaded with the below task

NSURLSessionDownloadTask *Imgtask =

[session downloadTaskWithURL:[NSURL URLWithString:imageUrl]

completionHandler:^(NSURL *loc,

NSURLResponse *resp,

NSError *error) {

UIImage *downloadedImage =

[UIImage imageWithData:

[NSData dataWithContentsOfURL:loc]];

dispatch_async(dispatch_get_main_queue(), ^{

_imageWithBlock.image = downloadedImage;



[Imgtask resume];



The following two tabs change content below.