已弃用:iOS9 中的 'sendAsynchronousRequest:queue:completionHandler:'

Deprecated: 'sendAsynchronousRequest:queue:completionHandler:' in iOS9

我有自己的 class 来进行 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: 是实例方法,不是 class 方法。您必须配置新会话或使用共享会话:

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:ourBlock] resume];
[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
 {
 // Block Body 
 }];
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in

// Code

}
task.resume()

如果您使用的是AFNetworking 库,您可以使用一个会话,然后在后台设置支持请求。使用这种方法我解决了两个问题:

1) AFNetworking 方法未弃用

2)即使应用在状态后台也会进行请求处理

希望对类似问题有所帮助。这是另一种选择。

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