AFNetworking 3.0 AFHTTPSessionManager文件下载

#import "ViewController.h"  
#import <AFNetworking.h>  
  
@interface ViewController ()  
  
- (IBAction)start:(UIButton *)sender;  
  
- (IBAction)pause:(UIButton *)sender;  
  
@property (weak, nonatomic) IBOutlet UIProgressView *pro;  
  
@property(nonatomic,strong) NSURLSessionDownloadTask *task;  
  
@end  
  
@implementation ViewController  
  
-(NSURLSessionDownloadTask *)task{  
      
    if (!_task) {  
          
        AFHTTPSessionManager *session=[AFHTTPSessionManager manager];  
          
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"]];  
          
        _task=[session downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {  
              
            //下载进度  
            NSLog(@"%@",downloadProgress);  
              
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{  
                  
                self.pro.progress=downloadProgress.fractionCompleted;  
                  
            }];  
              
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {  
              
            //下载到哪个文件夹  
            NSString *cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;  
              
            NSString *fileName=[cachePath stringByAppendingPathComponent:response.suggestedFilename];  
              
            return [NSURL fileURLWithPath:fileName];  
              
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {  
              
            //下载完成了  
            NSLog(@"下载完成了 %@",filePath);  
        }];  
    }  
      
    return _task;  
}  
  
- (void)viewDidLoad {  
    [super viewDidLoad];  
}  
  
- (void)didReceiveMemoryWarning {  
    [super didReceiveMemoryWarning];  
      
}  
  
- (IBAction)start:(UIButton *)sender {  
      
    [self.task resume];  
}  
  
- (IBAction)pause:(UIButton *)sender {  
      
    [self.task suspend];  
}  
@end  
原文地址:https://www.cnblogs.com/jyking/p/6737215.html