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

 

转载于:https://www.cnblogs.com/dashengios/p/10563430.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值