• 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  
    
  • 相关阅读:
    口袋摸球
    后渗透(四)数据包捕获
    后渗透(二)权限提升
    文件上传11-21
    MySQL之UDF提权
    《白帽子讲Web安全》读书笔记(一)
    XSS盲打获取cookies&&XSS钓鱼&&XSS键盘记录
    nginx目录穿越漏洞复现&&nginx目录遍历漏洞复现
    python教程(一)·简介
    DataPipeline如何实现数据质量管理?
  • 原文地址:https://www.cnblogs.com/jyking/p/6737215.html
Copyright © 2020-2023  润新知