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

 

posted on 2019-03-20 10:44  大圣ios博客  阅读(495)  评论(0编辑  收藏  举报