dataTaskWithRequest:completionHandler:已弃用

时间:2016-09-12 07:33:49

标签: ios objective-c afnetworking nsurlsessiondatatask

我在项目中使用AFNetworking从REST api获取数据。 但是当我使用Github中描述的方法时,我会收到警告。

这是我的代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];
NSURLRequest *request123 = [NSURLRequest requestWithURL:URL];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request123 completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];

[dataTask resume];

我的项目支持iOS 7.0及更高版本。

我的Xcode版本7.3.1

有没有办法克服这个警告?

2 个答案:

答案 0 :(得分:12)

现已在AFNetworking 3.0

中替换为以下内容
    - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
                           uploadProgress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
                         downloadProgress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock
                        completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler;

答案 1 :(得分:3)

此方法属于AFURLSessionManager。实际签名(使用两个块)是

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request 
                                      success:(void ( ^ ) ( NSURLResponse *response , id responseObject ))success 
                                      failure:(void ( ^ ) ( NSError *error ))failure