是千辛萬(wàn)苦總結(jié)出來(lái)的關(guān)于使用session來(lái)做完美的斷點(diǎn)下載,希望能幫到各位猿友####
此篇斷點(diǎn)下載的特色
1.支持?jǐn)帱c(diǎn)下載未蝌,這個(gè)必須的
2.支持多任務(wù)驮吱,任務(wù)數(shù)可以根據(jù)實(shí)際情況自行設(shè)置
3.支持后臺(tái)下載(session本身就支持,是不是覺(jué)得session很強(qiáng)大萧吠,加入session的隊(duì)列吧)
4.支持app意外退出保存斷點(diǎn)信息
5.每隔一秒更新一次進(jìn)度左冬,降低進(jìn)度條的更新頻率
使用到的第三方工具
AFNetworking
FMDB
好了廢話也不多說(shuō),直接上代碼怎憋,希望能幫到各位同行又碌,demo我一托管在github,有興趣的朋友可以看一下绊袋,里面會(huì)有一些我寫(xiě)這個(gè)demo時(shí)注意的一些東西,還有一些總結(jié)
demo演示
初始化下載單例
//注冊(cè)通知處理異常情況
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionDownloadAplicationWillTerminate) name:UIApplicationWillTerminateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
_lock = [[NSLock alloc] init];
_lock.name = @"ZBHTTPSessionShareTaskDict";
_taskDict = [NSMutableDictionary dictionary];
//后臺(tái)任務(wù)處理
_completionHandlerDictionary = [NSMutableDictionary dictionary];
_maxCount = 3;
_downloadingList = [NSMutableArray array];
_diskFileList = [NSMutableArray array];
//更新文件狀態(tài)铸鹰,防止重新運(yùn)行程序時(shí)癌别,上次未下載完成的任務(wù)可能會(huì)開(kāi)始下載
[FileModelDbManager updateUnFinishedFileState];
[_diskFileList addObjectsFromArray:[FileModelDbManager getAllDownloadedFile]];
[_downloadingList addObjectsFromArray:[FileModelDbManager getAllNotCompletedFile]];
[ZB_NetWorkShare ZB_NetWorkShare].backSessionCompletionDelegate = self;
下載關(guān)鍵代碼
for (FileModel *file in self.downloadingList) {
if (self.taskDict.count < self.maxCount) {
if (file.fileState == FileWillDownload) {
file.fileState = FileDownloading;
}
} else if(self.taskDict.count > self.maxCount){
if (file.fileState == FileDownloading) {
file.fileState = FileStopDownload;
}
} else {
}
if (file.fileState == FileDownloading) {
NSURLSessionTask *task = [self taskForKey:file.fileUrl];
if (!task) {
[self AF_BeginDownloadFileWithFileModel:file];
}
} else {
//此處未異步回掉self.downloadingcout--,不能及時(shí)生效蹋笼,所以更換另外一種方式
NSURLSessionDownloadTask *task = [self taskForKey:file.fileUrl];
if (task) {
[self removeTaskForKey:file.fileUrl];
if (file) {
file.fileState = FileStopDownload;
[FileModelDbManager insertFile:file];
}
[task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
}];
}
}
}
app意外退出斷點(diǎn)支持展姐,為了完成這個(gè)功能躁垛,樓主也是費(fèi)了9牛二虎之力,其中使用到了信號(hào)量圾笨,以及重新創(chuàng)建resumedata教馆,重新創(chuàng)建resumedata必須使用xml的參數(shù),否則會(huì)造成生成的resumedata文件不正確
NSDictionary *dict = [NSClassFromString(@"__NSCFLocalDownloadTask") getPropertiesDict];
for (NSString *obj in dict.allKeys) {
if ([@"downloadFile" isEqualToString:obj]) {
id propertyValue = [task valueForKeyPath:obj];
NSDictionary *downDict = [[propertyValue class] getPropertiesDict];
for (NSString *downProperty in downDict.allKeys) {
if ([@"path" isEqualToString:downProperty]) {
NSString *temFilePath = [propertyValue valueForKeyPath:downProperty];
NSData *resumeData = [self reCreateResumeDataWithTask:task tmFilePath:temFilePath];
[self handleResumeData:resumeData file:nil];
break;
}
}
break;
}
}
程序退出后臺(tái)時(shí)執(zhí)行的代碼擂达,由于程序退出的時(shí)候土铺,僅有短暫的幾秒時(shí)間app執(zhí)行任務(wù),而且不會(huì)執(zhí)行異步任務(wù)板鬓,所以這里用到了信號(hào)來(lái)取消后臺(tái)下載任務(wù)
dispatch_semaphore_t sem = dispatch_semaphore_create(1);
for (NSURLSessionDownloadTask *task in _taskDict.allValues) {
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
[task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
dispatch_semaphore_signal(sem);
}];
[self saveResumeDataWithTask:task];
}
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
還剩下關(guān)鍵的一步就可以支持app被kill時(shí)的斷點(diǎn)下載了悲敷,那就是重新生成resumedata
NSMutableDictionary *resumeDataDict = [NSMutableDictionary dictionary];
NSMutableURLRequest *newResumeRequest = [task.currentRequest mutableCopy];
NSData *tmData = [NSData dataWithContentsOfFile:tmFilePath];
[newResumeRequest addValue:[NSString stringWithFormat:@"bytes=%@-",@(tmData.length)] forHTTPHeaderField:@"Range"];
[resumeDataDict setObject:newResumeRequest.URL.absoluteString forKey:@"NSURLSessionDownloadURL"];
NSData *newResumeRequestData = [NSKeyedArchiver archivedDataWithRootObject:newResumeRequest];
NSData *oriData = [NSKeyedArchiver archivedDataWithRootObject:task.originalRequest];
[resumeDataDict setObject:@(tmData.length) forKey:@"NSURLSessionResumeBytesReceived"];
[resumeDataDict setObject:newResumeRequestData forKey:@"NSURLSessionResumeCurrentRequest"];
[resumeDataDict setObject:@(2) forKey:@"NSURLSessionResumeInfoVersion"];
[resumeDataDict setObject:oriData forKey:@"NSURLSessionResumeOriginalRequest"];
[resumeDataDict setObject:[tmFilePath lastPathComponent] forKey:@"NSURLSessionResumeInfoTempFileName"];
return [NSPropertyListSerialization dataWithPropertyList:resumeDataDict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];
相關(guān)鏈接
關(guān)于iOS的后臺(tái)下載和斷點(diǎn)續(xù)傳,說(shuō)一說(shuō)自己的理解
ios使用nsurlsession進(jìn)行下載
iOS中利用NSURLSession進(jìn)行文件斷點(diǎn)下載
NSURLSessionDownloadTask的深度斷點(diǎn)續(xù)傳
蘋(píng)果官方論壇
源碼我已上傳至github很方便朋友們下載demo
如果您有好的建議歡迎留言俭令,或者issue我后德,謝謝!