IOS开发网络篇之──ASIHTTPRequest示例(支持断点续传)

来源:互联网 发布:java jdbc 批量insert 编辑:程序博客网 时间:2024/06/03 18:59

  一、创建网络请求队列

首先,创建网络请求队列,如下:

<span style="font-size:14px;">ASINetworkQueue *que = [[ASINetworkQueue alloc] init];self.netWorkQueue = que;[que release];[self.netWorkQueue reset];[self.netWorkQueue setShowAccurateProgress:YES];[self.netWorkQueue go];</span>

  二、创建存放路径

<span style="font-size:14px;font-weight: normal;">//初始化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]; }</span>

三、发送下载请求

        这里对下面几个对象说明一下: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];//暂停请求这里的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];        }    }

 四、ASIHTTPRequestDelegate回调方法

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

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

#pragma mark -#pragma mark ASIHTTPRequestDelegate method//ASIHTTPRequestDelegate,下载之前获取信息的方法,主要获取下载内容的大小,可以显示下载进度多少字节- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)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)requestFinished:(ASIHTTPRequest *)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; }

    经过上述步骤,一个支持断点续传的功能程序实现了!

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买车下个月分期全部付清怎么办手续 天猫客服介入以后商家不退款怎么办 淘宝上买代购奢侈品买到假货怎么办 淘宝退货卖家收到货拒绝退款怎么办 没收到货但申请了退货退款怎么办 小米商城预约中德手机没货怎么办 电脑用百度网盘下载速度超慢怎么办 ios网盘下载速度太慢怎么办 小米手机4x卡机了怎么办 小米手机4x屏幕点不动了怎么办 苹果手机连接u盘没反应怎么办 苹果官网储蓄卡分期额度不够怎么办 京东买东西发票信息填写错了怎么办 华为v9手机激活密码忘了怎么办 公司报销发票纸质的丢了怎么办 在京东上买的小天才手表坏了怎么办 美亚买东西过几天降价了怎么办 天猫买东西退款后又不想退了怎么办 自己写错了地址快递被签收了怎么办 在天猫购买东西不发货怎么办 在天猫买了东西不给我发货怎么办 天猫退货卖家收到货不退款怎么办 唯品会的账号找不回来了怎么办 从网上买的沙发物流超级慢怎么办 三星c5手机玩王者荣耀卡怎么办 买手机送话费的卡不想用了怎么办 到银行存钱被骗买保险了怎么办 去银行存钱被骗买了保险怎么办 京东申请退款后卖家又发货了怎么办 苹果6s没有4g网怎么办 花呗分期付款买手机额度不够怎么办 手机天猫不支持购买淘宝商品怎么办 天猫国际购买商品狠盾怎么办 在微信上微拍堂买东西被骗了怎么办 京东自营买到返修手机怎么办? 7p弯了怎么办能修复吗 农商银行app登录密码忘了怎么办 网址上的重庆时时彩不能提现怎么办 天天中彩票自己账户登录不了怎么办 天猫上卖王者荣耀的兑换码是怎么办 身份证借别人开淘宝永久封店怎么办