
201406041133326.jpg
通過Download類來實(shí)現(xiàn)文件斷點(diǎn)下載
除了使用文件句柄以及輸出流之外,我們也可以通過download類來實(shí)現(xiàn)文件的斷點(diǎn)下載,但是需要注意的是,它只能實(shí)現(xiàn)斷點(diǎn)下載,是無法實(shí)現(xiàn)離線下載的。具體代碼如下所示:
@interface ViewController ()<NSURLSessionDownloadDelegate>
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSData *resumeData;
@property (nonatomic, strong)NSURLSession *session;
@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) {
//01 確定請(qǐng)求路徑
NSURL *url = [NSURL URLWithString:@"http://32812/resources/videos/minion_01.mp4"];
//02 創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//03 創(chuàng)建會(huì)話對(duì)象 設(shè)置代理
//04 創(chuàng)建請(qǐng)求任務(wù)
_downloadTask = [self.session downloadTaskWithRequest:request];
}
return _downloadTask;
}
//開始
- (IBAction)startBtnClick:(id)sender
{
[self.downloadTask resume];
NSLog(@"開始");
}
//暫停
- (IBAction)suspendBtnClick:(id)sender
{
[self.downloadTask suspend];
}
//取消
- (IBAction)cancelBtnClick:(id)sender
{
//取消 該取消方法是不能恢復(fù)下載
//[self.downloadTask cancel];
NSLog(@"取消---");
//該取消方法是可以恢復(fù)的
[self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
self.resumeData = resumeData;
}];
}
//恢復(fù)
- (IBAction)resumeBtnClick:(id)sender
{
//判斷是暫停之后恢復(fù)還是取消之后恢復(fù)
if (self.resumeData) {
//根據(jù)可恢復(fù)下載的數(shù)據(jù)來重新創(chuàng)建網(wǎng)絡(luò)請(qǐng)求
self.downloadTask = [self.session downloadTaskWithResumeData:self.resumeData];
self.resumeData = nil;
}
[self.downloadTask resume];
}
#pragma mark NSURLSessionDownloadDelegate
//01 寫數(shù)據(jù)的時(shí)候調(diào)用該方法
/*
bytesWritten :本次寫入數(shù)據(jù)的大小
totalBytesWritten:寫入數(shù)據(jù)的總大小
totalBytesExpectedToWrite:文件的總大小
*/
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
//計(jì)算文件的下載進(jìn)度
NSLog(@"%f",1.0 * totalBytesWritten / totalBytesExpectedToWrite);
}
//02 當(dāng)下載完成之后會(huì)調(diào)用
/*
location:文件的臨時(shí)存儲(chǔ)路徑
*/
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
//剪切文件到caches
//剪切文件到安全的位置
NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
//執(zhí)行文件剪切操作
[[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
NSLog(@"%@",fullPath);
}
//03 請(qǐng)求結(jié)束的時(shí)候調(diào)用
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"didCompleteWithError--%@",error);
}
@end