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

How Do You Change AFNetworking Request's Temporary FilePath

我正在尝试将我们的 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 执行此操作?

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 管理,通常它们是隐藏在您的文档目录中的原始文件。