- (void)download
{
// 1.创建会话管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com/img/bd_logo1.png"]];
//3.创建下载任务
/*
第一个参数:请求对象
第二个参数:进度回调 downloadProgress(获得进度信息)
第三个参数:destination
targetPath:文件的临时存储路径
response:响应头信息
返回值:NSURL(AFN内部已经实现了文件剪切的过程,但是需要我们告诉他应该把文件剪切到哪里)
第四个参数:completionHandler 请求完成的时候调用
response:响应头信息
filePath==fullPath 文件的最终目录
error:错误信息
*/
NSURLSessionDownloadTask *downloadTask = [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(@"---%@---\n%@",targetPath, fullPath);
//NSLog(@"%@--%@",[NSURL URLWithString:fullPath],[NSURL fileURLWithPath:fullPath]);
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"下载完成");
}];
//4.执行方法
[downloadTask resume];
}
AFN实现下载操作
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- title: 国外最新高清pdf寻找以及实现迅雷自动下载【Python】date: 2016-10-11 20:5...
- nginx 的第三方模块ngx_http_accesskey_module 来实现下载文件的防盗链步骤(linux...
- 尊重知识,转发请注明出处:iOS流媒体开发之三:HLS直播(M3U8)回看和下载功能的实现 概要流媒体开发第一篇文...
- JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪、客户服务、需求收集、流程审批、任务...
- 项目地址:https://github.com/Kulbear/All-IT-eBooks-Spider喜欢欢迎S...