zoukankan      html  css  js  c++  java
  • AFNetworking实现文件下载

    #import "ViewController.h"
    
    #import "AFNetworking/AFNetworking.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3782685451,3066622536&fm=27&gp=0.jpg"]];
        
        NSURLSessionDownloadTask *downTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
            //打印下载进度
            NSLog(@"%f",1.0 * downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            
            NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *fullPath = [filePath stringByAppendingPathComponent:response.suggestedFilename];
            
            NSLog(@"%@",fullPath);
            return [NSURL fileURLWithPath:fullPath];
            
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            
            NSLog(@"%@",filePath);
            NSLog(@"completionHandler----%@",error);
            
        }];
        
        [downTask resume];
        
    }
    
    @end
  • 相关阅读:
    1、怎么通过postman创建mock服务?
    13、使用正则表达式完成添加投资项目接口
    12、登录接口实战
    11、简历编写
    下载HTMLTestRunner 地址
    selenium (二)
    python 自动化测试
    Linux 安装MySQL流程
    文件 open 方法
    面向对象知识总结
  • 原文地址:https://www.cnblogs.com/dashengios/p/10565449.html
Copyright © 2011-2022 走看看