【问题标题】:How do I download and save a file locally on iOS using objective C? [duplicate]如何使用 Objective C 在 iOS 上本地下载和保存文件? [复制]
【发布时间】:2011-07-16 10:58:06
【问题描述】:

我是 Objective-C 的新手,我想从网络上下载一个文件(如果它在网络服务器上被更改)并将它保存在本地,以便我的应用程序可以使用它。

主要是我想实现wget --timestamp <url> 所做的事情。

【问题讨论】:

    标签: objective-c ios download wget local-storage


    【解决方案1】:

    我不确定 wget 是什么,但是要从网络上获取文件并将其存储在本地,您可以使用 NSData:

    NSString *stringURL = @"http://www.somewhere.com/thefile.png";
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    if ( urlData )
    {
      NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString  *documentsDirectory = [paths objectAtIndex:0];  
    
      NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
      [urlData writeToFile:filePath atomically:YES];
    }
    

    【讨论】:

    • 只是想知道,这是否阻塞?我会假设这一块。
    • @schystz 如果阻塞是指同步,那么是的。
    • 同样,大家可能也对+[NSString stringWithContentsOfURL:encoding:error:]感兴趣
    • 它将整个文件存储在内存中?如果是这样,这不是一个好的解决方案
    • 我存储在NSTemporaryDirectory (),无法播放视频.mp4。它非常适合音频和图像
    【解决方案2】:

    在 iOS 7 中引入的 NSURLSession 是推荐的 SDK 下载文件的方式。无需导入 3rd 方库。

    NSURL *url = [NSURL URLWithString:@"http://www.something.com/file"];
    NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:url];
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
    self.downloadTask = [self.urlSession downloadTaskWithRequest:downloadRequest];
    [self.downloadTask resume];
    

    然后您可以使用 NSURLSessionDownloadDelegate 委托方法来监控错误、下载完成、下载进度等...如果您愿意,也可以使用内联块完成处理程序回调方法。 Apples 文档解释了何时需要使用其中一个。

    阅读这些文章:

    objc.io NSURLConnection to NSURLSession

    URL Loading System Programming Guide

    【讨论】:

    【解决方案3】:

    我会使用一个使用完成块的异步访问。

    此示例将 Google 徽标保存到设备的文档目录中。 (iOS 5+,OSX 10.7+)

    NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [documentDir stringByAppendingPathComponent:@"GoogleLogo.png"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com/images/srpr/logo11w.png"]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error) {
            NSLog(@"Download Error:%@",error.description);
        }
        if (data) {
            [data writeToFile:filePath atomically:YES];
            NSLog(@"File is saved to %@",filePath);
        }
    }];
    

    【讨论】:

    • sendAsynchronousRequest 自 iOS 9.0 起已弃用。
    【解决方案4】:

    我认为更简单的方法是使用 ASIHTTPRequest。三行代码即可完成:

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:@"/path/to/my_file.txt"];
    [request startSynchronous];
    

    Link to Reference

    更新:我应该提到 ASIHTTPRequest 不再维护。作者特别建议大家改用其他框架,比如AFNetworking

    【讨论】:

      【解决方案5】:

      前段时间我实现了一个易于使用的“下载管理器”库:PTDownloadManager。你可以试一试!

      【讨论】:

      • 你很好。谢谢。
      【解决方案6】:

      有很多方法:

      1. NSURL

      2. ASIHTTP

      3. libcurl

      4. easyget,功能强大的商业版。

      【讨论】:

      猜你喜欢
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多