不推荐使用:' sendAsynchronousRequest:queue:completionHandler:'在iOS9中

时间:2015-07-04 10:26:45

标签: ios objective-c xcode ios9

我有自己的类来进行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];
}

有什么想法吗?

4 个答案:

答案 0 :(得分:16)

var Dlg = ((Microsoft.Office.Interop.Word.Application)Word).get_FileDialog(MsoFileDialogType.msoFileDialogFilePicker); Dlg.InitialFileName = STRfolderroot + STRfoldertemplatescommon + "\\" + TheModality + "\\" + TheModality + " " + TheStudyType + "\\"; Dlg.Show(); 是一个实例方法,而不是类方法。您必须配置新会话或使用共享会话:

dataTaskWithRequest:completionHandler:

答案 1 :(得分:8)

[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
 {
 // Block Body 
 }];

答案 2 :(得分:1)

let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in

// Code

}
task.resume()

答案 3 :(得分:1)

如果您使用的是 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];
}