ios NSURLConnection initWithRequest 已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32647138/
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-08-31 07:41:59  来源:igfitidea点击:

NSURLConnection initWithRequest is deprecated

iosgmail-apixcode7

提问by Aditya Borde

I am adopting the Gmail APIin iOS and I am getting the warning:

我在 iOS 中采用Gmail API,但收到警告:

initWithRequest is deprecated

initWithRequest 已弃用

in the following line:

在以下行中:

connection_ = [[connectionClass alloc] initWithRequest:request_ delegate:self startImmediately:NO];

The line is in the source file GTMHTTPFetcher.m of the API library.

该行位于 API 库的源文件 GTMHTTPFetcher.m 中。

What is the substitute for the deprecated -initWithRequest:method?

什么是已弃用-initWithRequest:方法的替代品?

采纳答案by Nicolas Miari

It seems that the whole NSURLConnectionAPI has been deprecated in iOS 9. Existing apps will continue to work, but new builds (linked against iOS SDK) must use the newer NSURLSessionAPI.

整个NSURLConnectionAPI似乎在 iOS 9 中已被弃用。现有应用程序将继续工作,但新版本(与 iOS SDK 相关联)必须使用较新的NSURLSessionAPI。

Ray Wenderlich has a good tutorial here. Also, of course, check the official documentation.

Ray Wenderlich 有一个很好的教程在这里。另外,当然,请查看官方文档

回答by Raphael

NSURLConnectionis deprecated in iOS 9. You can use NSURLSessioninstead which exists since iOS 7.

NSURLConnection在 iOS 9 中已弃用。您可以使用NSURLSession自 iOS 7 以来存在的替代。

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
        {
            // do something with the data 
        }];
[dataTask resume];

回答by Randel S

If you don't care about the completionHandler : here's an one liner.

如果您不关心 completionHandler :这里是one liner

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

回答by Paresh Navadiya

Use STHTTPRequestwhich uses NSURLConnection/NSURLSession.

使用STHTTPRequest它使用NSURLConnection的/ NSURLSession

For NSURLSessionuse STHTTPRequest2.

对于NSURLSession使用STHTTPRequest2

STHTTPRequestis best library as it has only 2 files and easy to use.

STHTTPRequest是最好的库,因为它只有 2 个文件并且易于使用。