AF3.0.4如何实现文件下载和保存
1、工程导入AFNetworking3.0.4库之后,新建NetWorkingManager类继承于AFHTTPSessionManager,然后导入AFNetworking.h头文件,如下:


2、在NetWorkingManager.h文件中声明外部调用方法:
- (void )downLoadFileWithUrl:(NSString *)url progress:(BlockProgress )progressBlock completionBlock:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))block;
url:为下载的url。
progress:为下载的回调进度。
block:为回调信息block。

3、在NetWorkingManager.m里面定义AFHTTPSessionManager实例化方法,方便其他的方法进行参数设置,如下:

4、实例化AFHTTPSessionManager获取到sessionManager,使用NSURLRequest获取到request,如下:
AFHTTPSessionManager *sessionManager = [self requestBaseHttp];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

5、AF使用downloadTaskWithRequest方法进行下载,如下:
[sessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
}];

6、destination下必须设置下载保存的路径,如下:
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
return [NSURL URLWithString:filePath];
在completionHandler回调中filePath即为下载保存的文件路径,使用下载的文件从filePath下读取即可,如下:
