`
wsqwsq000
  • 浏览: 675510 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ASIHTTPRequest下载示例(支持断点续传)

阅读更多

 

 

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

    一、创建网络请求队列

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

    ASINetworkQueue   *que = [[ASINetworkQueue alloc] init];

    self.netWorkQueue = que;

    [que release];

 

    [self.netWorkQueue reset];

    [self.netWorkQueue setShowAccurateProgress:YES];

    [self.netWorkQueue go];

  二、创建存放路径

    //初始化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];

}

        三、发送下载请求

        这里对下面几个对象说明一下: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; 

}

 

 

 

分享到:
评论

相关推荐

    ASIHTTPRequest断点续传

    ASIHTTPRequest实现资源的下载,断点续传

    DownAndASIRequest下载,断点续传

    一款ios实现下载,断点续传的小例子,利用asihttprequest写的,值得学习哦。。。

    IOS 多线程异步下载,断点续传

    asihttprequest下载例子,集进度条和字节数显示,多线程异步下载,断点续传,解压缩

    断点续传队列和本地持久化

    1.基于ASIHttpRequest的断点续传功能,进一步封装,自定义了下载队列,包括下载中、已完成队列,并且用CoreData对其持久化封装,使用简单,资源url作为标示,传入网络资源url和本地path即可。 2.队列使用字典进行...

    IOS基于ASIHttpRequest的封装,使用单例模式断点续传

    IOS基于ASIHttpRequest的封装,使用单例模式断点续传,只需传入要下载的url,即可返回一定时间内的下载进度,错误回调等

    ASIHTTPRequest+UITableView实现多个下载任务

    ASIHTTPRequest+UITableView实现多个下载任务,没用到重用机制,还有没有实现断点续载,很简单的一个demo,相信初学者都能看懂,还写了一些注释。

    ASIHttpRequest 下载显示有进度条的alert框

    该demo最主要的功能是在用ASIHttpRequest 下载,(当然同样也适用于上传),在弹出的alert框中显示下载(或是上传数据)的进度 在这里注意:如果是下载较大的文件,进度条会缓慢进行;相反如果下载的文件很小,那...

    ASIHTTPRequest

    ASIHTTPRequest,封装了http传输,使用简单

    ASIHTTPRequest 最新版本 包 下载

    使用iOS SDK中的HTTP网络请求API,相当的复杂,调用很繁琐,ASIHTTPRequest就是一个对CFNetwork API进行了封装,并且使用起来非常简单的一套API,用Objective-C编写,可以很好的...l 支持断点续传 l 支持同步和异步请求

    ASIHTTPRequest.framework支持虚拟机和真机

    ASIHTTPRequest.framework支持虚拟机和真机

    ASIHttpRequest

    利用ASIHttpRequest实现客户端向服务器端请求登陆验证的示例 博客参考:http://blog.csdn.net/dingxiaowei2013/article/details/12617203

    IOS ASIHttpRequest资源包

    ASIHTTPRequest是简单易用的,它封装了CFNetwork API。使得与Web服务器通信变得更简单。它是用Objective-C编写的,可以在MAC OS X和iPhone应用中使用。 它适用于执行基本的HTTP请求和互动(或者说是反馈)。...

    ASIHttpRequest示例

    利用ASIHttpRequest实现的手机客户端向服务器端的登陆验证 博客参考:http://blog.csdn.net/dingxiaowei2013/article/details/12617203

    ASIHTTPRequest框架

    此为ASIHTTPRequest打包的Frameword,直接导入到工程即可使用。

    ASIHttpRequest下载

    ASIHttpRequest 功能强大的HTTP连接的类库,你可以用它的ASIHTTPRequest或ASIFormDataRequest来访问第三方提供的接口。

Global site tag (gtag.js) - Google Analytics