[英]Deprecated: 'sendAsynchronousRequest:queue:completionHandler:' in iOS9
I have my own class to make http calls but now in iOS9 this method is deprecated:我有自己的类来进行 http 调用,但现在在 iOS9 中此方法已弃用:
[NSURLConnetion sendAsynchronousRequest:queue:completionHandler:]
I'm trying to test the new one [NSURLSession dataTaskWithRequest:completionHandler:]
我正在尝试测试新的
[NSURLSession dataTaskWithRequest:completionHandler:]
but Xcode give an error because it doesn't found this method.但是 Xcode 给出了一个错误,因为它没有找到这个方法。
Xcode compiler warning with deprecated line:带有弃用行的 Xcode 编译器警告:
'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. dataTaskWithRequest:completionHandler:
是一个实例方法,而不是一个类方法。 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.如果您使用的是AFNetworking库,则可以使用会话,然后设置为在后台支持请求。 Using this approach I solved two problems:
使用这种方法我解决了两个问题:
1) AFNetworking method is not deprecated 1) 不推荐使用 AFNetworking 方法
2) the request processing will be done even if the application between the state background 2) 即使应用程序在状态后台之间也会进行请求处理
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];
}
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
// Code
}
task.resume()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.