Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 316
EnriMR Avatar asked Jul 04 '15 10:07

EnriMR


3 Answers

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] resume];
like image 105
Léo Natan Avatar answered Oct 19 '22 23:10

Léo Natan


[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
 {
 // Block Body 
 }];
like image 20
Kishor Kumar Rawat Avatar answered Oct 19 '22 23:10

Kishor Kumar Rawat


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.

-(void) pingSomeWebSite {

    NSString* url = URL_WEBSITE; // defines your URL to PING

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"GET"];
    [request setURL:[NSURL URLWithString:url]];
    request.timeoutInterval = DEFAULT_TIMEOUT; // defines your time in seconds

    NSTimeInterval  today = [[NSDate date] timeIntervalSince1970];
    NSString *identifier = [NSString stringWithFormat:@"%f", today];

    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
    sessionConfig.timeoutIntervalForResource = DEFAULT_TIMEOUT_INTERVAL; // interval max to background 

    __block AFURLSessionManager *manager = [[AFURLSessionManager alloc]
                                            initWithSessionConfiguration:
                                            sessionConfig];

    NSURLSessionTask *checkTask = [manager dataTaskWithRequest:request
                                             completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

        NSLog(@"- Ping to - %@", URL_WEBSITE);

        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
        dispatch_async(dispatch_get_main_queue(), ^(){

            [manager invalidateSessionCancelingTasks:YES];

            // LOGIC FOR RESPONSE CODE HERE 
        });
    }];

    [checkTask resume];
}
like image 28
Douglas Frari Avatar answered Oct 19 '22 23:10

Douglas Frari