系统NSURLSessionDownloadTask实现简单的断点下载

其实系统提供的下载并不算困难, 也许以前使用NSURLConnection确实有点麻烦, 但是苹果推出的NSSURLSession并不复杂,用起来也比较简单.

  • 1.首先用到一个代理就是NSURLSessionDownloadDelegate
    通过初始化NSURLSessionConfiguration来配置sesson
NSURL *url = [NSURL URLWithString:@"http://dlsw.baidu.com/sw-search-sp/soft/9d/25765/sogou_mac_32c_V3.2.0.1437101586.dmg"];
    //初始sesson化配置对象
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    //初始化sesson
    self.sessons = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    //创建下载任务
    self.downLoadTask = [self.sessons downloadTaskWithURL:url];
  • 2.当然sessons并不能自动启动下载, 需要手动启动,通过[self.downLoadTask resume];的方法启动

需要用到的两个代理方法,其他的代理方法可以自己去研究

/**
 *  下载过程,多次调用,存储下载状态
 *
 *  @param bytesWritten              每次下载的字节数
 *  @param totalBytesWritten         当前下载的总字节数
 *  @param totalBytesExpectedToWrite 下载的文件的总字节数
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{

}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{

    //创建文件存储路径,downloadTask.response.suggestedFilename为所下载的文件名
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager moveItemAtPath:location.path toPath:file error:nil];
    //下载完成提示框(UIAlertView再Xcode7后就废除了)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"下载完成" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];

}
  • 3.还有两个方法用来控制暂停和重新开始
    暂停下载
    [self.downLoadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
    //resumeData存储的下载状态,当恢复下载时候需要使用
    }];
    重新开始下载
    self.downLoadTask = [self.sessons downloadTaskWithResumeData:self.resumData];
    [self.downLoadTask resume];
- (IBAction)pushOrPlay:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        //判断是否是恢复下载
        if (self.myPregress.progress != 0) {
            //恢复下载是调用的方法
        self.downLoadTask = [self.sessons downloadTaskWithResumeData:self.resumData];
        }
        [self.downLoadTask resume];
        [sender setTitle:@"暂停" forState:(UIControlStateNormal)];
    }else{
        //暂停下载时调用的block块
        __weak typeof (self) weakSelf = self;
        [self.downLoadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
            //resumeData存储的下载状态,当恢复下载时候需要使用
            weakSelf.resumData = resumeData;
        }];
        [sender setTitle:@"下载" forState:(UIControlStateNormal)];
    }
}

效果图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值