在iOS9中不推荐使用NSURLConnection [英] NSURLConnection deprecated in iOS9

查看:127
本文介绍了在iOS9中不推荐使用NSURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个带有 NSURLRequest 的文件,并将其保存在

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

<的行中code> 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];

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

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

推荐答案

现在你必须使用 NSURLSession

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

现在你需要调用那个方法动作(如果你是你的完整网址)更喜欢)和API调用返回时将执行的块。

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.

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

在该区块内,您将收到 NSData 使用响应数据,使用HTTP响应 NSURLResponse 。现在,您可以将代码放在那里:

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

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

[data writeToURL:documentsURL atomically:YES];

NSURLSession和NSURLConnection之间的主要区别


  • NSURLConnection:如果我们与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.

NSURLSession:解决了这个问题,也让我们没有进程下载。即使我们没有访问权限,它也会管理连接过程。您需要在AppDelegate中使用应用程序:handleEventsForBackgroundURLSession:completionHandler

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:completionHandler in your 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.

这篇关于在iOS9中不推荐使用NSURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆