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

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值