//
// ViewController.m
// downLoadTask代理
//
// Created by 泛在吕俊衡 on 2017/6/16.
// Copyright © 2017年 anjohnlv. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<NSURLSessionDataDelegate,NSURLSessionTaskDelegate, NSURLSessionDelegate>
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSData *resumeData;
@end
@implementation ViewController
/*
NSURLSessionDownloadTask:可以在后台继续下载,适用下载一些大文件
NSURLSessionDataTask:不可以在后台继续下载,适用一帮数据获取
NSURLSessionDataTask:适用于上传文件
*/
-(NSData *)resumeData {
if (_resumeData==nil) {
_resumeData=[[NSData alloc] init];
}
return _resumeData;
}
-(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://120.25.226.186:32812/resources/videos/minion_01.mp4"];
NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url];
_downloadTask =[self.session downloadTaskWithRequest:request];
}
return _downloadTask;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)start:(id)sender {
// 开始下载
[self.downloadTask resume];
}
- (IBAction)pause:(id)sender {
//暂停下载
[self.downloadTask suspend];
}
- (IBAction)cancle:(id)sender {
//取消下载
[self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
// 当前已经下载的内容
self.resumeData=resumeData;
}];
}
- (IBAction)goon:(id)sender {
// 继续下载
if (self.resumeData.length>0) {
self.downloadTask= [self.session downloadTaskWithResumeData:self.resumeData];
}
[self.downloadTask resume];
}
/* Sent periodically to notify the delegate of download progress. */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
//下载进度
NSLog(@"offet:%f", 1.0*totalBytesWritten/totalBytesExpectedToWrite);
}
/* Sent when a download has been resumed. If a download failed with an
* error, the -userInfo dictionary of the error will contain an
* NSURLSessionDownloadTaskResumeData key, whose value is the resume
* data.
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes{
NSLog(@"offet:%lli %lli", fileOffset, expectedTotalBytes);
}
/* Sent when a download task that has completed a download. The delegate should
* copy or move the file at the given location to a new location as it will be
* removed when the delegate message returns. URLSession:task:didCompleteWithError: will
* still be called.
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {
//文件临时下载地址
NSLog(@"location:%@",location);
NSString *filepath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
// 将文件移到本地文件夹内
[[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL URLWithString:filepath] error:nil];
NSLog(@"%@",filepath);
}
@end
ios -断点下载
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 尊重知识,转发请注明出处:基于iOS 10、realm封装的下载器(支持存储读取、断点续传、后台下载、杀死APP重...
- 在项目里遇到附件的下载和本地查看功能,附件有可能是word pdf 图片 Excel表格 甚至是ppt 有点变态吧...
- 从iOS7以来,苹果推出NSURLSession后,iOS现在可以实现真正的后台下载,这对我们iOSer来说是一个...