EnriMR EnriMR - 1 year ago 362
iOS Question

Deprecated: 'sendAsynchronousRequest:queue:completionHandler:' in iOS9

I have my own class to make http calls but now in iOS9 this method is deprecated:

[NSURLConnetion sendAsynchronousRequest:queue:completionHandler:]

I'm trying to test the new one
[NSURLSession dataTaskWithRequest:completionHandler:]

but Xcode give an error because it doesn't found this method.

Xcode compiler warning with deprecated line:

'sendAsynchronousRequest:queue:completionHandler:' is deprecated: first deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h

Error with new method:

No known class method for selector 'dataTaskWithRequest:completionHandler:'


-(void)placeGetRequest:(NSString *)action withHandler:(void (^)(NSURLResponse *response, NSData *data, NSError *error))ourBlock {

NSString *url = [NSString stringWithFormat:@"%@/%@", URL_API, action];

NSURL *urlUsers = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:urlUsers];

//[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:ourBlock];
[NSURLSession dataTaskWithRequest:request completionHandler:ourBlock];

Any idea?

Answer Source

dataTaskWithRequest:completionHandler: is an instance method, not a class method. You have to either configure a new session or use the shared one:

[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:ourBlock];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download