#import "AFNetworking.h"
//创建会话管理者
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
/*下载文件
参数1:请求对象
参数2:进度
参数3:回调位置
targetPath:临时文件路径
response;响应头信息
参数4:completionHandler:完成回调
filePath:文件路径*/
NSURL *url = [NSURL URLWithString:@"www.baidu.com/大文件.mp4"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//监听下载进度
NSLog(@"%f",1.0 *downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"targetPath:%@",targetPath);//打印下载临时路径
NSLog(@"fullPath:%@",fullPath);//打印指定下载路径
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"filePath:%@",filePath);//打印下载完成文件路径
}];
//执行task
[download resume];
网友评论