EnriMR EnriMR - 5 months ago 166
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:'


Method:

-(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

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];
Comments