ios NSURLConnection 在 iOS9 中被弃用

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

NSURLConnection deprecated in iOS9

iosnsurlconnectionnsurlsessionios9

提问by Maximilian

I want to download a file with a NSURLRequestand save it but in the line with the

我想下载一个带有 a 的文件NSURLRequest并保存它,但与

NSData * data = ...happens an error.

NSData * data = ...发生错误。

NSURL *Urlstring = [NSURL URLWithString:@"http://yourdomain.com/yourfile.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL: Urlstring];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
documentsURL = [documentsURL URLByAppendingPathComponent:@"localFile.pdf"];

[data writeToURL:documentsURL atomically:YES];

The warning Message is that i should use NSURLSession dataTaskwithrequest"because sendSynchronousRequestis deprecated in iOS 9 but that doesn't work I hope someone can help me

警告消息是我应该使用NSURLSession dataTaskwithrequest“因为sendSynchronousRequest在 iOS 9 中已弃用,但这不起作用我希望有人可以帮助我

回答by EnriMR

Now you have to use NSURLSession

现在你必须使用 NSURLSession

Example (GET):

示例(获取):

-(void)placeGetRequest:(NSString *)action withHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))ourBlock {

    NSString *urlString = [NSString stringWithFormat:@"%@/%@", URL_API, action];


    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

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

Now you will need to call that method with an action (or your full URL if you prefer) and the block that will be executed when the API call return.

现在,您需要使用操作(或您的完整 URL,如果您愿意)和 API 调用返回时将执行的块来调用该方法。

[self placeGetRequest:@"action" withHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // your code
}];

Inside that block, you will received a NSDatawith the response data and NSURLResponsewith the HTTP response. So now, you can put your code there:

在该块内,您将收到NSData带有响应数据和NSURLResponseHTTP 响应的 。所以现在,你可以把你的代码放在那里:

NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
documentsURL = [documentsURL URLByAppendingPathComponent:@"localFile.pdf"];

[data writeToURL:documentsURL atomically:YES];

Main difference between NSURLSession and NSURLConnection

NSURLSession 和 NSURLConnection 的主要区别

  • NSURLConnection: if we have an open connection with NSURLConnection and the system interrupt our App, when our App goes to background mode, everything we have received or sent were lost. Process diagram for NSURLConnection

  • NSURLSession: solve this problem and also give us out of process downloads. It manage the connection process even when we don't have access. You will need to use application:handleEventsForBackgroundURLSession:completionHandlerin your AppDelegate Process diagram for NSURLSession

  • NSURLConnection:如果我们与 NSURLConnection 有一个开放的连接并且系统中断了我们的应用程序,当我们的应用程序进入后台模式时,我们收到或发送的所有内容都会丢失。 NSURLConnection 的流程图

  • NSURLSession:解决这个问题,也给我们提供进程外下载。即使我们没有访问权限,它也会管理连接过程。您将需要application:handleEventsForBackgroundURLSession:completionHandler在您的 AppDelegate 中使用 NSURLSession 的流程图

So with the use of NSURLSession, you don't need to manage or to check your internet connection because OS does it for you.

因此,通过使用 NSURLSession,您无需管理或检查您的互联网连接,因为操作系统会为您完成。