在iOS9中不建议使用:"sendAsynchronousRequest:queue:completionHandler:" [英] Deprecated: 'sendAsynchronousRequest:queue:completionHandler:' in iOS9

查看:58
本文介绍了在iOS9中不建议使用:"sendAsynchronousRequest:queue:completionHandler:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的类来进行http调用,但是现在在iOS9中,该方法已被弃用:

I have my own class to make http calls but now in iOS9 this method is deprecated:

[NSURLConnetion sendAsynchronousRequest:queue:completionHandler:]

我正在尝试测试新的 [NSURLSession dataTaskWithRequest:completionHandler:]

I'm trying to test the new one [NSURLSession dataTaskWithRequest:completionHandler:]

但是Xcode给出了一个错误,因为它找不到此方法.

but Xcode give an error because it doesn't found this method.

Xcode编译器警告和不建议使用的行:

Xcode compiler warning with deprecated line:

 'sendAsynchronousRequest:queue:completionHandler:' is deprecated: first deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h

新方法出错:

No known class method for selector 'dataTaskWithRequest:completionHandler:'

方法:

-(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];
}

有什么主意吗?

推荐答案

dataTaskWithRequest:completionHandler:是实例方法,而不是类方法.您必须配置一个新会话或使用共享的会话:

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

这篇关于在iOS9中不建议使用:"sendAsynchronousRequest:queue:completionHandler:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆