IOS - 基于AFN下载文件

1.创建下载模型TNDownloadModel

    /**
     *  下载文件
     *
     *  @param downloadURL  下载链接
     *  @param success 请求结果
     *  @param faliure 错误信息
     */
    +(void)downloadURL:(NSString *) downloadURL progress:(void (^)(NSProgress *downloadProgress))progress destination:(void (^)(NSURL *targetPath))destination failure:(void(^)(NSError *error))faliure{
        

        //1.创建管理者
        AFHTTPSessionManager *manage  = [AFHTTPSessionManager manager];
        
        //2.下载文件
        /*
         第一个参数:请求对象
         第二个参数:下载进度
         第三个参数:block回调,需要返回一个url地址,用来告诉AFN下载文件的目标地址
         targetPath:AFN内部下载文件存储的地址,tmp文件夹下
         response:请求的响应头
         返回值:文件应该剪切到什么地方
         第四个参数:block回调,当文件下载完成之后调用
         response:响应头
         filePath:文件存储在沙盒的地址 == 第三个参数中block的返回值
         error:错误信息
         */
        
        //2.1 创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: downloadURL]];
        
        NSURLSessionDownloadTask *downloadTask = [manage downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {//进度
            
                if (downloadProgress) {
                    progress(downloadProgress);
                }
            
            } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            
                NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
                //拼接文件全路径
                NSString *fullpath = [caches stringByAppendingPathComponent:response.suggestedFilename];
                NSURL *filePathUrl = [NSURL fileURLWithPath:fullpath];
                
                return filePathUrl;
            
            } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nonnull filePath, NSError * _Nonnull error) {
                
                
                if (error) {
                    faliure(error);
                }
                
                
                if(filePath){
                    
                    destination(filePath);
                }
        }];
        
        //3.启动任务
        [downloadTask resume];
    }

2.下载事件响应

 -(void)upDownload{


  @weakify(self);
  [TNDownloadModel downloadElectronicProtocolContract:self.contract progress:^(NSProgress *downloadProgress) {
    
        //下载进度
         NSString *progress = [NSString stringWithFormat:@"下载:%f%%",100.0 * downloadProgress.completedUnitCount/downloadProgress.totalUnitCount];
    
  } destination:^void(NSURL *targetPath) {//下载完毕

        //targetPath下载完成的本地存储路径
        //下载完成后的处理
    
     } failure:^(NSError *error) {
        //下载过程中遇到的错误
    }];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,871评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,463评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,631评论 25 709
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,366评论 30 472
  • API定义规范 本规范设计基于如下使用场景: 请求频率不是非常高:如果产品的使用周期内请求频率非常高,建议使用双通...
    有涯逐无涯阅读 7,748评论 0 6