IOS开发之ASIHTTPRequest下载示例

在工程中,我们会常常遇到需要下载的程序,比如下载在线音乐、下载图片等等,今天我将介绍一下利用ASIHTTPRequest的下载示例,支持断点续传,利用ASIHTTPRequest下载以及断点续传的原理,今天重点介绍如何实现,废话少说,开始正文:

  一、创建网络请求队列

  首先,创建网络请求队列,如下:
 
  1.  ASINetworkQueue *que = [[ASINetworkQueue alloc] init];
  2.   self.netWorkQueue = que;
  3.   [que release];
  4.   [self.netWorkQueue reset];
  5.   [self.netWorkQueue setShowAccurateProgress:YES];
  6.   [self.netWorkQueue go];
复制代码

  二、创建存放路径

  1.   //初始化Documents路径
  2.   NSString *path = [NSHomeDirectory() stringByAppendingPathComponent"Documents"];
  3.   //初始化临时文件路径
  4.   NSString *folderPath = [path stringByAppendingPathComponent"temp"];
  5.   //创建文件管理器
  6.   NSFileManager *fileManager = [NSFileManager defaultManager];
  7.   //判断temp文件夹是否存在
  8.   BOOL fileExists = [fileManager fileExistsAtPath:folderPath];
  9.   if (!fileExists) {//如果不存在说创建,因为下载时,不会自动创建文件夹
  10.   [fileManager createDirectoryAtPath:folderPath
  11.   withIntermediateDirectories:YES
  12.   attributes:nil
  13.   error:nil];
  14.   }
复制代码

  三、发送下载请求

  这里对下面几个对象说明一下:CustomCell是我自定义的cell,cell上面有下载和暂停两个按钮,其tag值为cell所在的行,因此这里的[sendertag]为下载按钮的tag值,self.downloadArray为array数组对象,存放要下载的资源字典信息,在该字典中有一键为URL,它对应的值就是我们下载链接。

  这些东西,根据自己的实际需要改动一下即可使用

  1.   CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
  2.   NSString *filePath = [[self.downloadArray objectAtIndex:[sender tag]] objectForKey"URL"];
  3.   NSLog(@"filePath=%@",filePath);
  4.   //初始下载路径
  5.   NSURL *url = [NSURL URLWithString:filePath];
  6.   //设置下载路径
  7.   ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
  8.   //设置ASIHTTPRequest代理
  9.   request.delegate = self;
  10.   //初始化保存ZIP文件路径
  11.   NSString *savePath = [path stringByAppendingPathComponent:[NSString stringWithFormat"book_%d.zip",[sender tag]]];
  12.   //初始化临时文件路径
  13.   NSString *tempPath = [path stringByAppendingPathComponent:[NSString stringWithFormat"temp/book_%d.zip.temp",[sender tag]]];
  14.   //设置文件保存路径
  15.   [request setDownloadDestinationPath:savePath];
  16.   //设置临时文件路径
  17.   [request setTemporaryFileDownloadPath:tempPath];
  18.   //设置进度条的代理,
  19.   [request setDownloadProgressDelegate:cell];
  20.   //设置是是否支持断点下载
  21.   [request setAllowResumeForFileDownloads:YES];
  22.   //设置基本信息
  23.   [request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[sender tag]],@"bookID",nil]];
  24.   NSLog(@"UserInfo=%@",request.userInfo);
  25.   //添加到ASINetworkQueue队列去下载
  26.   [self.netWorkQueue addOperation:request];
  27.   //收回request
  28.   [request release];
复制代码


  三、暂停请求

  这里的cell下下载时的一样,
  1.   CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
  2.   for (ASIHTTPRequest *request in [self.netWorkQueue operations]) {
  3.   NSInteger bookid = [[request.userInfo objectForKey"bookID"] intValue];//查看userinfo信息
  4.   if ([sender tag] == bookid) {//判断ID是否匹配
  5.   //暂停匹配对象
  6.   [request clearDelegatesAndCancel];
  7.   }
  8.   }
复制代码

  四、ASIHTTPRequestDelegate回调方法

  上面已经把下载请求与暂停请求实现,点击下载时,开始下载资源;当点暂停时,下载中断;当我们再点击下载按钮时,继续下载,在第二步的

  [request setAllowResumeForFileDownloads:YES]设置是是否支持断点下载。下面要实现ASIHTTPRequestDelegate代理方法如下:

  1.   #pragma mark -
  2.   #pragma mark ASIHTTPRequestDelegate method
  3.   //ASIHTTPRequestDelegate,下载之前获取信息的方法,主要获取下载内容的大小,可以显示下载进度多少字节
  4.   - (void)requestASIHTTPRequest *)request didReceiveResponseHeadersNSDictionary *)responseHeaders {
  5.   NSLog(@"didReceiveResponseHeaders-%@",[responseHeaders valueForKey"Content-Length"]);
  6.   NSLog(@"contentlength=%f",request.contentLength/1024.0/1024.0);
  7.   int bookid = [[request.userInfo objectForKey"bookID"] intValue];
  8.   NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  9.   float tempConLen = [[userDefaults objectForKey:[NSString stringWithFormat"book_%d_contentLength",bookid]] floatValue];
  10.   NSLog(@"tempConLen=%f",tempConLen);
  11.   //如果没有保存,则持久化他的内容大小
  12.   if (tempConLen == 0 ) {//如果没有保存,则持久化他的内容大小
  13.   [userDefaults setObject:[NSNumber numberWithFloat:request.contentLength/1024.0/1024.0] forKey:[NSString stringWithFormat"book_%d_contentLength",bookid]];
  14.   }
  15.   }
  16.   //ASIHTTPRequestDelegate,下载完成时,执行的方法
  17.   - (void)requestFinishedASIHTTPRequest *)request {
  18.   int bookid = [[request.userInfo objectForKey"bookID"] intValue];
  19.   CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:bookid inSection:0]];
  20.   cell.downloadCompleteStatus = YES;
  21.   cell.progressView.progress = 0.0;
  22.   }
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值