Deprecated: 'sendAsynchronousRequest:queue:completionHandler:' in iOS9
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];
[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
{
// Block Body
}];
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];
}