gpt4 book ai didi

ios - AFNetworking 3.0如何下载文件并保存到本地?

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:18 37 4
gpt4 key购买 nike

在我的项目中,我需要下载一个小视频。在以前的版本中,我使用这个:

- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag;

如何在 AFNetworking 3.0 中执行此操作?

最佳答案

这段代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];

在项目的自述文件中:https://github.com/AFNetworking/AFNetworking

关于ios - AFNetworking 3.0如何下载文件并保存到本地?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35137749/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com