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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31219548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:25:16  来源:igfitidea点击:

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

iosobjective-cxcodeios9

提问by EnriMR

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

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

[NSURLConnetion sendAsynchronousRequest:queue:completionHandler:]

[NSURLConnetion sendAsynchronousRequest:queue:completionHandler:]

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

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

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

但是 Xcode 给出了一个错误,因为它没有找到这个方法。

Xcode compiler warning with deprecated line:

带有弃用行的 Xcode 编译器警告:

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

Error with new method:

新方法错误:

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

Method:

方法:

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

Any idea?

任何的想法?

回答by Leo Natan

dataTaskWithRequest:completionHandler:is an instance method, not a class method. You have to either configure a new session or use the shared one:

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

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

回答by Kishor Kumar Rawat

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

回答by Douglas Frari

If you are using the AFNetworkinglibrary, you can use a session and then set up to support requests in the background. Using this approach I solved two problems:

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

1) AFNetworking method is not deprecated

1) 不推荐使用 AFNetworking 方法

2) the request processing will be done even if the application between the state background

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

I hope it helps to similar problems. This is an alternative.

我希望它有助于解决类似的问题。这是一个替代方案。

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

回答by deepak vaishnav

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

// Code

}
task.resume()