AFN实现文件下载

#import "ViewController.h"

#import "AFNetworking.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self download];

}

 

-(void)download

{

    //1.创建会话管理者

    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

    

    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];

    

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

    //2.下载文件

    /*

     第一个参数:请求对象

     第二个参数:progress 进度回调 downloadProgress

     第三个参数:destination 回调(目标位置)

                有返回值

                targetPath:临时文件路径

                response:响应头信息

     第四个参数:completionHandler 下载完成之后的回调

                filePath:最终的文件路径

     */

    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

        

        //监听下载进度

        //completedUnitCount 已经下载的数据大小

        //totalUnitCount     文件数据的中大小

        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);

    }];

    

    //3.执行Task

    [download resume];

}

 

 

@end

原文地址:https://www.cnblogs.com/liuzhenjie/p/5480237.html