我们平时实际开发中文件下载一般都用AFNetworking
比较多,NSURLSessionDataTask
刚开始学习iOS开发的时候用过,很多年没有使用过了,之前有的那点印象已经模糊的不能再模糊了,我们今天简单回顾一下,回到那个羞涩的秋天,
思路(相关源码网上有很多,我这里重点是思路和容易犯错的点)
我们再iOS开发中使用网络请求一般都是使用
NSURLSession
类,实际使用中使用它的子类:NSURLSessionDataTask
、NSURLSessionUploadTask
、NSURLSessionDownloadTask
,第一个就是我们今天要使用的类创建
NSURLSessionDataTask
对象,然后调用它的resume
方法之后网络请求就发送成功了,代理里面接受NSData
数据就可以了(不能直接使用alloc来创建,切记)
NSURLSessionDataTask *task = [session dataTaskWithRequest:mutiRequest];
[task resume];
- 上面需要一个
NSURLSession
对象,我们就创建这个类
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:queue];
-
delegate
这个默认是NSURLSessionDelegate
,但是我们不能使用这个,需要子类的代理回调NSURLSessionTaskDelegate
,相关代码我放下面哈 -
NSURLSessionConfiguration
类是相关设置,正常是使用默认,还有两个选择,感兴趣的可以看看,有个后台模式应该有应用的场景
- 接下来就是那个请求类
NSMutableURLRequest
NSMutableURLRequest *mutiRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com/105/media/cn/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-cn-20170912_1280x720h.mp4"]];
NSString *range = [NSString stringWithFormat:@"bytes=%f-",self.currentLength];
[mutiRequest setValue:range forHTTPHeaderField:@"Range"];
- 这里必须使用可变请求类,因为可变请求类才有设置下载文件的范围
-
@"bytes=%f-"
这个位置,byte
后面是有s
,我刚开始看别人的demo没有写s
调试的时候一直出错,加了就好了,这个也是我今天想写这篇文章的主要原因 -
@"Range"
这里是固定值
delegate
回调
- 回调1:返回一个响应头,block回调告知是否允许下载
- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveResponse:(nonnull NSURLResponse *)response completionHandler:(nonnull void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSLog(@"----开始----%lld---%@",response.expectedContentLength,response.MIMEType);
self.totalLength = response.expectedContentLength+self.currentLength;
// 允许处理服务器的响应,才会继续接收服务器返回的数据
if (self.currentLength < self.totalLength) {
completionHandler(NSURLSessionResponseAllow);
}else{
completionHandler(NSURLSessionResponseCancel);
}
}
-
response.expectedContentLength
剩余下载量,我们上面传了已经下载的文件大小,这里会返回剩余文件的大小 -
completionHandler
这个block回调应该不用多解释了哈
- 调用2:接受数据,写入数据,因为我们使用的是新建的队列,所以这里是子线程,更新UI操作需要回到主线程
- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data{
[self.fileHandle writeData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%",100.0*dataTask.countOfBytesReceived/dataTask.countOfBytesExpectedToReceive];
});
}
- 温馨提示这个方法调用的频率很高,不要把一些计算量较大的代码写在这里
- 回调3:任务结束,无错误
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error == nil) {
NSLog(@"----下载完成-----%@",error);
}
// if (task.state == NSURLSessionTaskStateCompleted) {
// NSURLSessionTaskStateRunning = 0,
// NSURLSessionTaskStateSuspended = 1,
// NSURLSessionTaskStateCanceling = 2,
// NSURLSessionTaskStateCompleted = 3,
// }
}
全部代码
@interface ViewController ()<NSURLSessionTaskDelegate>
@property (nonatomic , strong) NSFileHandle *fileHandle;
@property (nonatomic , strong) NSURLSessionDataTask *dataTask;
@property (nonatomic , strong) UILabel *progressLabel;
@property (nonatomic , copy) NSString *path;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.redColor;
_progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 50)];
_progressLabel.center = self.view.center;
_progressLabel.backgroundColor = UIColor.lightGrayColor;
_progressLabel.textAlignment = NSTextAlignmentCenter;
_progressLabel.text = @"0.00%";
[self.view addSubview:_progressLabel];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (_dataTask) {
[_dataTask cancel];
_dataTask = nil;
}else{
self.currentLength = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.path error:nil] fileSize];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:queue];
NSMutableURLRequest *mutiRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com/105/media/cn/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-cn-20170912_1280x720h.mp4"]];
NSString *range = [NSString stringWithFormat:@"bytes=%f-",self.currentLength];
[mutiRequest setValue:range forHTTPHeaderField:@"Range"];
NSURLSessionDataTask *task = [session dataTaskWithRequest:mutiRequest];
[task resume];
_dataTask = task;
}
}
- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveResponse:(nonnull NSURLResponse *)response completionHandler:(nonnull void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSLog(@"----开始----%lld---%@",response.expectedContentLength,response.MIMEType);
self.totalLength = response.expectedContentLength+self.currentLength;
// 允许处理服务器的响应,才会继续接收服务器返回的数据
completionHandler(NSURLSessionResponseAllow);
//completionHandler(NSURLSessionResponseCancel);
}
- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data{
[self.fileHandle writeData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%",100.0*dataTask.countOfBytesReceived/dataTask.countOfBytesExpectedToReceive];
});
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error == nil) {
NSLog(@"----下载完成-----%@",error);
}
// if (task.state == NSURLSessionTaskStateCompleted) {
// NSURLSessionTaskStateRunning = 0,
// NSURLSessionTaskStateSuspended = 1,
// NSURLSessionTaskStateCanceling = 2,
// NSURLSessionTaskStateCompleted = 3,
// }
}
- (NSFileHandle *)fileHandle
{
if (!_fileHandle) {
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:self.path]) {
[fm createFileAtPath:self.path contents:nil attributes:nil];
}
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.path];
[_fileHandle seekToEndOfFile];
}
return _fileHandle;
}
- (NSString *)path
{
if (!_path) {
_path = [NSString stringWithFormat:@"/Users/XXXXXXX/Desktop/hahaha/iphone%u.mp4",arc4random()%9999];
}
return _path;
}
简单的一个断点续传,感兴趣的小伙伴可以把全部代码直接copy过去,修改一下保存文件的路径就可以运行