• 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  
    
  • 相关阅读:
    发张照片纪念下
    以用户为中心的SNS站点数据库设计及实现
    IT点评:腾讯帝国没落的开始从崇敬到平淡,改变从自己开始
    并发下常见的加锁及锁的PHP具体实现
    Ubuntu,Your Linux
    Python初尝试
    C++ Primer Plus读书笔记02
    C++ Primer Plus读书笔记03
    Effective C++读书笔记02
    由扔骰子看平均概率生成
  • 原文地址:https://www.cnblogs.com/jyking/p/6737215.html
Copyright © 2020-2023  润新知