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?
dataTaskWithRequest:completionHandler:
is an instance method, not a class method. You have to either configure a new session or use the shared one:If you are using the AFNetworking library, you can use a session and then set up to support requests in the background. Using this approach I solved two problems:
1) AFNetworking method is not deprecated
2) the request processing will be done even if the application between the state background
I hope it helps to similar problems. This is an alternative.