//
// 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
网友评论