【问题标题】:Download file in iPhone Device在 iPhone 设备中下载文件
【发布时间】:2015-09-09 09:48:56
【问题描述】:

我有一个文件 URL,想在我的 iPhone 的文档目录中下载它。这样我就可以在其他应用程序中使用这个下载的文件,比如在 WhatsApp 上共享它,在电子邮件附件等中。

我正在使用 ASIHTTPRequest,我应该设置什么目标路径?

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.ImageURL];
[request setDownloadDestinationPath:@"What Path I should Set"];
[request startSynchronous];

这是我要下载的文件的示例 url: http://www.fourspan.com/apps/agentreview/public/img/ads/1441791507_Muhammad.jpg

【问题讨论】:

    标签: ios objective-c download asihttprequest


    【解决方案1】:

    首先ASIHTTPRequest已经过时,请避免使用。

    这是使用AFNetworking 下载任何文件并将其保存到您想要的任何位置的代码(savePath

    #define kDocumentsDirPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
    
    - (void)downloadFile:(NSString *)filePath
    {
    
    AFURLSessionManager *sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:filePath]];
    
    NSURLSessionDownloadTask *downloadTask = [sessionManager downloadTaskWithRequest:request
                                                                            progress:nil
                                                                         destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    
                                                                             NSString *savePath = [kDocumentsDirPath stringByAppendingPathComponent:fileName];
    
                                                                             return [NSURL fileURLWithPath:savePath];
    
                                                                         }
                                                                   completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    
                                                                       if (error)
                                                                       {
                                                                           NSLog(@"Download failed for %@: %@", fileName, error.localizedDescription);
    
                                                                           [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Failed to download %@", fileName]
                                                                                                      message:error.localizedDescription
                                                                                                     delegate:nil
                                                                                            cancelButtonTitle:@"Uh Oh"
                                                                                             otherButtonTitles:nil] show];
                                                                       }
                                                                       else {
    
                                                                           NSLog(@"File downloaded: %@", fileName);
                                                                       }
                                                                   }];
    [downloadTask resume];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多