如何更改AFNetworking请求的临时文件路径

时间:2015-03-17 13:21:28

标签: ios objective-c download afnetworking afnetworking-2

我正在尝试将ASIHTTPRequest代码迁移到AFNetworking。我对POST请求没问题,但我遇到下载请求问题。我似乎无法设置要下载的内容的临时文件路径。在ASIHTTPRequest中,我可以得到这样的代码:

    // Create file path
    NSString *filePath = [cachePath stringByAppendingPathComponent:package.fileName];
    NSString *tempFile = [tempPath stringByAppendingPathComponent:package.fileName];

    [downloadRequest setTemporaryFileDownloadPath:tempFile];
    [downloadRequest setDownloadDestinationPath:filePath];

如何使用AFNetworking完成此操作?

1 个答案:

答案 0 :(得分:0)

AFURLSessionManager* urlSessionManager = [AFURLSessionManager.alloc initWithSessionConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
NSProgress* progress = nil;
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://your.remote.file"]];
NSURLSessionDownloadTask* downloadTask = [urlSessionManager downloadTaskWithRequest:request progress:&progress destination:^NSURL* (NSURL* targetPath, NSURLResponse* response) {
    NSURL* targetFileUrl = [NSURL fileURLWithPath:@"/your/local/path" isDirectory:NO];
    return targetFileUrl;
} completionHandler:^(NSURLResponse* response, NSURL* filePath, NSError* error) {
    if (error)
    {
        // Some error occurred or download programmatically cancelled
    }
    else
    {
        // Download completed
    }
}];
[downloadTask resume];

临时文件由AFNetworking管理,通常他们在文档目录中隐藏了原始文件。