Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch from NSURLConnection to NSURLSession in Objective-C

I have this code in Objective-C which works with the initWithRequest of NSURLConnection class and Xcode gives me the following warning:

initWithRequest is deprecated: first deprecated in iOS9 - Use NSURLSession

My ViewController.m code is as follows:

NSString url = "my url"; 
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
conexion = [[NSURLConnection alloc]initWithRequest:req delegate:self];

Conexion is NSURLConnection in ViewController.h, so i was wondering how to switch from NSURLConnection to NSURLSession.

like image 341
miguelacio Avatar asked Nov 24 '15 01:11

miguelacio


People also ask

How do I use NSURLSession in Objective C?

NSURLSession introduces a new pattern to Foundation delegate methods with its use of completionHandler: parameters. This allows delegate methods to safely be run on the main thread without blocking; a delegate can simply dispatch_async to the background, and call the completionHandler when finished.

What is difference between NSURLConnection and NSURLSession?

NSURLSession also provides nicer interfaces for requesting data using blocks, in that it allows you to combine them with delegate methods for doing custom authentication handling, redirect handling, etc., whereas with NSURLConnection, if you suddenly realized you needed to do those things, you had to refactor your code ...

Is NSURLSession asynchronous?

Like most networking APIs, the NSURLSession API is highly asynchronous. It returns data to your app in one of three ways, depending on the methods you call: If you're using Swift, you can use the methods marked with the async keyword to perform common tasks.

What is NSURLConnection in swift?

An NSURLConnection object lets you load the contents of a URL by providing a URL request object. The interface for NSURLConnection is sparse, providing only the controls to start and cancel asynchronous loads of a URL request. You perform most of your configuration on the URL request object itself. Note.


1 Answers

If you want to continue to use delegate-based API, like your current implementation, it's:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

NSURL *URL = [NSURL URLWithString:string];

NSURLSessionTask *task = [session dataTaskWithURL:URL];
[task resume];

If you do that, you have to conform to NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate methods, much like your current NSURLConnectionDataDelegate and NSURLConnectionDelegate methods.

By the way, I show dataTaskWithURL above, but if you really need to use NSURLRequest (e.g. you're going to do POST or other request), there is dataTaskWithRequest, too.

Having said that, using the completion handler rendition is much easier, saving you have having to write all of those delegate methods:

NSURLSession *session = [NSURLSession sharedSession];

NSURL *URL = [NSURL URLWithString:string];

NSURLSessionTask *task = [session dataTaskWithURL:URL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // process response here
}];
[task resume];

If you do this, you don't have to implement any of those delegate methods. It's analogous to the NSURLConnection convenience method sendAsynchronousRequest, except it is cancellable.

And, just like the above dataTaskWithURL, there are also download and upload specific renditions, downloadTaskWithURL and uploadTaskWithRequest.

For more information, see Using NSURLSession in the URL Session Programming Guide.

like image 80
Rob Avatar answered Nov 05 '22 14:11

Rob