在工程中,咱们会经常遇到须要下载的程序,好比下载在线音乐、下载图片等等,今天我将介绍一下利用ASIHTTPRequest的下载示例,支持断点续传,利用ASIHTTPRequest下载以及断点续传的原理,今天重点介绍如何实现,废话少说,开始正文:
1、建立网络请求队列
首先,建立网络请求队列,以下:
数组
ASINetworkQueue *que = [[ASINetworkQueue alloc] init];
网络
self.netWorkQueue = que;
url
[que release];
spa
[self.netWorkQueue reset];
.net
[self.netWorkQueue setShowAccurateProgress:YES];
代理
[self.netWorkQueue go];orm
复制代码对象
2、建立存放路径
队列
//初始化Documents路径
图片
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent"Documents"];
//初始化临时文件路径
NSString *folderPath = [path stringByAppendingPathComponent"temp"];
//建立文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//判断temp文件夹是否存在
BOOL fileExists = [fileManager fileExistsAtPath:folderPath];
if (!fileExists) {//若是不存在说建立,由于下载时,不会自动建立文件夹
[fileManager createDirectoryAtPath:folderPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
复制代码
3、发送下载请求
这里对下面几个对象说明一下:CustomCell是我自定义的cell,cell上面有下载和暂停两个按钮,其tag值为cell所在的行,所以这里的[sendertag]为下载按钮的tag值,self.downloadArray为array数组对象,存放要下载的资源字典信息,在该字典中有一键为URL,它对应的值就是咱们下载连接。
这些东西,根据本身的实际须要改动一下便可使用
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
NSString *filePath = [[self.downloadArray objectAtIndex:[sender tag]] objectForKey"URL"];
NSLog(@"filePath=%@",filePath);
//初始下载路径
NSURL *url = [NSURL URLWithString:filePath];
//设置下载路径
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
//设置ASIHTTPRequest代理
request.delegate = self;
//初始化保存ZIP文件路径
NSString *savePath = [path stringByAppendingPathComponent:[NSString stringWithFormat"book_%d.zip",[sender tag]]];
//初始化临时文件路径
NSString *tempPath = [path stringByAppendingPathComponent:[NSString stringWithFormat"temp/book_%d.zip.temp",[sender tag]]];
//设置文件保存路径
[request setDownloadDestinationPath:savePath];
//设置临时文件路径
[request setTemporaryFileDownloadPath:tempPath];
//设置进度条的代理,
[request setDownloadProgressDelegate:cell];
//设置是是否支持断点下载
[request setAllowResumeForFileDownloads:YES];
//设置基本信息
[request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[sender tag]],@"bookID",nil]];
NSLog(@"UserInfo=%@",request.userInfo);
//添加到ASINetworkQueue队列去下载
[self.netWorkQueue addOperation:request];
//收回request
[request release];
复制代码
3、暂停请求
这里的cell下下载时的同样,
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
for (ASIHTTPRequest *request in [self.netWorkQueue operations]) {
NSInteger bookid = [[request.userInfo objectForKey"bookID"] intValue];//查看userinfo信息
if ([sender tag] == bookid) {//判断ID是否匹配
//暂停匹配对象
[request clearDelegatesAndCancel];
}
}
复制代码
4、ASIHTTPRequestDelegate回调方法
上面已经把下载请求与暂停请求实现,点击下载时,开始下载资源;当点暂停时,下载中断;当咱们再点击下载按钮时,继续下载,在第二步的
[request setAllowResumeForFileDownloads:YES]设置是是否支持断点下载。下面要实现ASIHTTPRequestDelegate代理方法以下:
#pragma mark -
#pragma mark ASIHTTPRequestDelegate method
//ASIHTTPRequestDelegate,下载以前获取信息的方法,主要获取下载内容的大小,能够显示下载进度多少字节
- (void)requestASIHTTPRequest *)request didReceiveResponseHeadersNSDictionary *)responseHeaders {
NSLog(@"didReceiveResponseHeaders-%@",[responseHeaders valueForKey"Content-Length"]);
NSLog(@"contentlength=%f",request.contentLength/1024.0/1024.0);
int bookid = [[request.userInfo objectForKey"bookID"] intValue];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
float tempConLen = [[userDefaults objectForKey:[NSString stringWithFormat"book_%d_contentLength",bookid]] floatValue];
NSLog(@"tempConLen=%f",tempConLen);
//若是没有保存,则持久化他的内容大小
if (tempConLen == 0 ) {//若是没有保存,则持久化他的内容大小
[userDefaults setObject:[NSNumber numberWithFloat:request.contentLength/1024.0/1024.0] forKey:[NSString stringWithFormat"book_%d_contentLength",bookid]];
}
}
//ASIHTTPRequestDelegate,下载完成时,执行的方法
- (void)requestFinishedASIHTTPRequest *)request {
int bookid = [[request.userInfo objectForKey"bookID"] intValue];
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:bookid inSection:0]];
cell.downloadCompleteStatus = YES;
cell.progressView.progress = 0.0;
}
复制代码