zoukankan      html  css  js  c++  java
  • 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  
    
  • 相关阅读:
    解析大型.NET ERP系统 20条数据库设计规范
    vi显示行号
    shell awk
    Linux使用Shell脚本实现ftp的自动上传下载
    MySQL Replication的Reset slave重置命令
    怎么样调整FreeBSD时区问题
    Basic Memory Structures
    States of Integrity Constraints
    Merging into a Table: Example
    oracle 单独开始一个事物的写法 。
  • 原文地址:https://www.cnblogs.com/jyking/p/6737215.html
Copyright © 2011-2022 走看看