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
.
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.
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 ...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With