关于使用ASIHTTPRequest进行断点续传
来源:互联网 发布:lol骚男的淘宝店网址 编辑:程序博客网 时间:2024/05/15 10:39
最近项目中用到了ASI的断点续传,把其中遇到的问题和解决方法与大家分享
NSString *bookPath = [RBUtilsObject filePathString:s9id]; ASIHTTPRequest *bookDownloadRequest = [[ASIHTTPRequest alloc]initWithURL:requestURL]; bookDownloadRequest.shouldContinueWhenAppEntersBackground = YES; [bookDownloadRequest setDownloadDestinationPath:bookPath]; NSString *tmpbookPath = [RBUtilsObject tmpfilePathString:s9id]; [bookDownloadRequest setTemporaryFileDownloadPath:tmpbookPath]; [bookDownloadRequest setAllowResumeForFileDownloads:YES]; bookDownloadRequest.allowCompressedResponse = NO; __block NSString *currentDownloadBookID = s9id; __block BOOL isAddToShelf = NO; [bookDownloadRequest setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) { if ( total !=0) { if (isAddToShelf == NO) { isAddToShelf = YES; [[NSNotificationCenter defaultCenter]postNotificationName:@(kIsAddToShelf) object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:currentDownloadBookID,@(kCurrentDownloadBookID), nil]];//s9id } float progressAmount = (float)((size*1.0)/(total*1.0)); NSLog(@"downloadPercent=%f s9id= %@",progressAmount,currentDownloadBookID); //这里的size其实是request接收到的数据的回调block,并不是当前全部已下载的数据,因此在这样不能正确显示下载进度 [[NSNotificationCenter defaultCenter]postNotificationName:currentDownloadBookID object:@(progressAmount)]; } }];
解决方案如下:找到ASIHTTPRequest的.m文件
- (void)updateDownloadProgress{// We won't update download progress until we've examined the headers, since we might need to authenticateif (![self responseHeaders] || [self needsRedirect] || !([self contentLength] || [self complete])) {return;}unsigned long long bytesReadSoFar = [self totalBytesRead]+[self partialDownloadSize];unsigned long long value = 0;if ([self showAccurateProgress] && [self contentLength]) {value = bytesReadSoFar-[self lastBytesRead];if (value == 0) {return;}} else {value = 1;[self setUpdatedProgress:YES];}if (!value) {return;}[ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&queue withObject:self amount:&value callerToRetain:self];[ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&downloadProgressDelegate withObject:self amount:&value callerToRetain:self];[ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]];#if NS_BLOCKS_AVAILABLE if (bytesReceivedBlock) {unsigned long long totalSize = [self contentLength] + [self partialDownloadSize];//[self performBlockOnMainThread:^{ if (bytesReceivedBlock) { bytesReceivedBlock(value, totalSize); }}];此处修改为即可将下载进度正确返回 [self performBlockOnMainThread:^{ if (bytesReceivedBlock) { bytesReceivedBlock(([self totalBytesRead] + [self partialDownloadSize]),totalSize); } }]; }#endif[self setLastBytesRead:bytesReadSoFar];}
0 0
- 关于使用ASIHTTPRequest进行断点续传
- ASIHTTPRequest 断点续传
- ASIHTTPRequest 断点续传
- ASIHTTPRequest-断点续传
- ASIHTTPRequest-断点续传
- 断点续传 ASIHTTPRequest
- ASIHTTPRequest 断点续传
- 断点续传 ASIHTTPRequest
- ASIHttpRequest 断点续传
- iOS 使用NSURLSession进行断点续传
- iphone开发--使用ASIHTTPRequest实现下载,断点续传功能
- 在IOS中使用ASIHTTPRequest下载(支持断点续传)
- iOS 使用ASIHTTPRequest实现图片下载,断点续传,cell自定义
- iOS开发asi使用(五)ASIHTTPRequest断点续传(下载)
- ASIHTTPRequest的使用——大文件断点续传
- ASIHTTPRequest-断点续传(下载)
- ASIHTTPRequest-断点续传(下载)
- ASIHTTPRequest-断点续传(下载)
- 如何判断复杂语言引擎支持情况
- 奋战吧!测试11加油!八组加油!
- Apache Thrift 配置环境
- PopupWindow的使用细节以及今天遇到的小问题
- PHP中MVC框架的实现
- 关于使用ASIHTTPRequest进行断点续传
- Python 深入理解yield
- 纯干货!一款APP从设计稿到切图过程全方位揭秘
- getAttribute 和 getParameter的区别
- 顺序表应用1:多余元素删除之移位算法
- ubuntu 安装jdk1.7
- JavaScript 运行机制详解:再谈Event Loop
- mysql 5.6 存储过程+事务+游标+错误异常抛出+日志写入
- Linux中对文件描述符的操作(FD_ZERO、FD_SET、FD_CLR、FD_ISSET