• NSURLSession使用NSURLSessionDownloadTask实现大文件下载,监听下载进度


    感觉到法国

    感受到附近开个户口登记

    #import "ViewController.h"
    
    @interface ViewController ()<NSURLSessionDownloadDelegate>
    
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation ViewController
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
    //    [self download];//小文件下载
        [self downloadDelegate];//大文件下载用这个
    }
    
    -(void)download
    {//文件下载, 该方法无法监听下载进度
        NSURL *url = [NSURL URLWithString:@"http://www.ytmp3.cn/down/59224.mp3"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        NSURLSession *session = [NSURLSession sharedSession];
        
        NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            NSLog(@"%@",location);
            
            NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *fullPath = [cachePath stringByAppendingPathComponent:response.suggestedFilename];
            //将文件转移到安全的地方去
            [[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
            
            NSLog(@"----%@",fullPath);
            
        }];
        [downloadTask resume];
    }
    
    -(void)downloadDelegate
    {
        NSURL *url = [NSURL URLWithString:@"http://www.ytmp3.cn/down/59224.mp3"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        //使用这个方法为session设置代理,监听下载进度和过程
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
        NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
        
        [downloadTask resume];
        
    }
    
    #pragma mark NSURLSessionDownloadDelegate
    
    //bytesWritten 本次写入数据的大小
    //totalBytesWritten 写入数据的总大小
    //totalBytesExpectedToWrite 文件的总大小
    -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
    {
        //下载进度
        NSLog(@"----%f", 1.0 * totalBytesWritten/totalBytesExpectedToWrite);
    }
    
    -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
    {//下载完成调用
        
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *fullPath = [cachePath stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
        //将文件转移到安全的地方去
        [[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
        
        NSLog(@"----%@",fullPath);
        
    }
    
    -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
    {//整个请求完成或者请求失败调用
        
        NSLog(@"didCompleteWithError");
    }
    @end
  • 相关阅读:
    恐怖的东西,C#进入前四,进入正轨。
    JavaScript高级培训-自定义对象
    优秀程序员无他善假于物也
    Java基础知识之泛型全接触
    Haskell函数式编程之二递归
    敏捷的写博客
    生命、宇宙及万物的终极答案《图灵的秘密》读后感
    Java基础知识之Enum全接触
    WebDriver自动化测试利器剖析
    Haskell函数式编程之三纯函数式编程特点
  • 原文地址:https://www.cnblogs.com/dashengios/p/10563430.html
Copyright © 2020-2023  润新知