演示效果如下: 利用NSURLSession實現斷點下載 1.屬性 2.初始化屬性 3.實現點擊事件 4.實現代理方法 //保存恢複數據 self.resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData]; 怎麼實現的斷點下載? ...
演示效果如下:
利用NSURLSession實現斷點下載
1.屬性
@interface ViewController ()<NSURLSessionDownloadDelegate> /** 下載任務 */ @property (nonatomic,strong) NSURLSessionDownloadTask *task; /** 上次的下載信息 */ @property (nonatomic,strong) NSData *resumeData; /** session */ @property (nonatomic,strong) NSURLSession *session; @end
2.初始化屬性
- (NSURLSession *)session { if (!_session) { _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; } return _session; } - (NSData *)resumeData { //第一次訪問resumeData載入沙盒中的resumeData if (!_resumeData) { NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *file = [caches stringByAppendingPathComponent:@"data"]; _resumeData = [NSData dataWithContentsOfFile:file]; } return _resumeData; }
3.實現點擊事件
/** * 開始下載 */ - (IBAction)start:(id)sender { //獲得下載任務 self.task = [self.session downloadTaskWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_02.mp4"]]; //啟動任務 [self.task resume]; } /** * 暫停下載 */ - (IBAction)pause:(id)sender { //一旦這個task被取消了,就無法再恢復 //data裡面存儲著上次的下載信息 [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) { self.resumeData = resumeData; //可以將resumeData放到沙盒中 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *file = [caches stringByAppendingPathComponent:@"data"]; [resumeData writeToFile:file atomically:YES]; }]; } /** * 繼續下載 */ - (IBAction)goOn:(id)sender { self.task = [self.session downloadTaskWithResumeData:self.resumeData]; [self.task resume]; }
4.實現代理方法
#pragma mark -<NSURLSessionDownloadDelegate> - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes { NSLog(@"%s",__func__); } /** * 每當寫入數據到臨時文件中時,就會調用一次 * totalBytesExpectedToWrite:總大小 * totalBytesWritten:已經寫入的大小 * bytesWritten 這次寫入大小 */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { NSLog(@"%s",__func__); NSLog(@"------%f",1.0*totalBytesWritten/totalBytesExpectedToWrite); } /** * 下載完畢就會調用一次這個方法 */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSLog(@"%s",__func__); //文件將來存放的真實路徑 NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename]; //剪切location的臨時文件到真實路徑 NSFileManager *manager = [NSFileManager defaultManager]; [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil]; } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { NSLog(@"%s",__func__);
//保存恢複數據
self.resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData];
}
怎麼實現的斷點下載?
resumeData裡面保存了上次下載的信息
/**
* 請求這個http://120.25.226.186:32812/resources/videos/minion_02.mp4路徑
* 設置請求頭
* Range: 0-100
* [[NSMutableURLRequest requestWithURL:nil] setValue:@"0-1024" forHTTPHeaderField:@"Range"];
* 通過設置Range這個請求頭可以控制下載哪一部分的信息
*/
這些在resumeData都幫你做好了