我有自己的类可以进行http调用,但是现在在iOS9中不赞成使用此方法:
[NSURLConnetion sendAsynchronousRequest:queue:completionHandler:]
我正在尝试测试新的[NSURLSession dataTaskWithRequest:completionHandler:]
但是Xcode给出了一个错误,因为它找不到此方法。

Xcode编译器警告,不建议使用以下行:

 '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:是实例方法,而不是类方法。您必须配置一个新 session 或使用共享的 session :

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:ourBlock] resume];

关于ios - 弃用的: 'sendAsynchronousRequest:queue:completionHandler:' in iOS9,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31219548/

10-11 19:27