NSURLSessionDownloadTask实现断点下载,对下载任务进行暂停,取消,恢复

NSURLSessionDownloadTask不能实现离线断点下载

因为该代理方法是直接将下载结果返回,而不是分步存储磁盘,因此离线断点下载可以使用NSURLSessionDataTask

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDownloadDelegate>

@property(nonatomic,strong) NSURLSessionDownloadTask *downloadTask;
@property(nonatomic,strong) NSURLSession *session;
@property(nonatomic,strong) NSData  *fileInfoData;//取消下载时保存的数据

@end

@implementation ViewController

-(NSURLSession *)session
{
    if (_session == nil) {
        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    }
    return _session;
}

-(NSURLSessionDownloadTask *)downloadTask
{
    if (_downloadTask == nil) {
        
        NSURL *url = [NSURL URLWithString:@"http://www.ytmp3.cn/down/59224.mp3"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        _downloadTask = [self.session downloadTaskWithRequest:request];
    }
    return _downloadTask;
}

- (IBAction)startBtnClick:(UIButton *)sender {
    //开始
    NSLog(@"开始下载");
    [self.downloadTask resume];
    
}
- (IBAction)suspendBtnclick:(UIButton *)sender {
    //暂停
    [self.downloadTask suspend];
    NSLog(@"暂停下载");
}
- (IBAction)cancelBtnClick:(UIButton *)sender {
    
    //取消 , [self.downloadTask cancel]普通的取消是不能恢复的.
    //resumeData 可以用来恢复下载的数据
    [self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
        self.fileInfoData = resumeData;
    }];
    self.downloadTask = nil;//取消后 将downloadTask置空。
}
- (IBAction)resumeBtnClick:(UIButton *)sender {
    //恢复下载
    //如果已经取消了下载,需要拿到可恢复的数据重新创建下载任务
    if (self.fileInfoData.length > 0) {
        //根据可恢复数据重新创建一个新的downloadTask指针指向全局下载任务
        self.downloadTask = [self.session downloadTaskWithResumeData:self.fileInfoData];
        [_downloadTask resume];
        
    }else {
        [self.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 11:58  大圣ios博客  阅读(1778)  评论(0编辑  收藏  举报