简体   繁体   English

在iOS9中不推荐使用NSURLConnection

[英]NSURLConnection deprecated in iOS9

I want to download a file with a NSURLRequest and save it but in the line with the 我想下载一个带有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 sendSynchronousRequest is deprecated in iOS 9 but that doesn't work I hope someone can help me 警告消息是,我应该使用NSURLSession dataTaskwithrequest “因为sendSynchronousRequest在iOS的9弃用但是,这并不工作,我希望有人能帮助我

Now you have to use NSURLSession 现在你必须使用NSURLSession

Example (GET): 示例(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 NSData with the response data and NSURLResponse with the HTTP response. 在该块中,您将收到带有响应数据的NSData和带有HTTP响应的NSURLResponse 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];

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. NSURLConnection:如果我们与NSURLConnection打开连接并且系统中断我们的应用程序,当我们的应用程序进入后台模式时,我们收到或发送的所有内容都将丢失。 NSURLConnection的流程图

  • NSURLSession: solve this problem and also give us out of process downloads. NSURLSession:解决这个问题,也让我们脱离进程下载。 It manage the connection process even when we don't have access. 即使我们没有访问权限,它也会管理连接过程。 You will need to use application:handleEventsForBackgroundURLSession:completionHandler in your AppDelegate 您需要在AppDelegate中使用application:handleEventsForBackgroundURLSession:completionHandler 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,您无需管理或检查您的Internet连接,因为操作系统会为您执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM