在工程中,咱們會經常遇到須要下載的程序,好比下載在線音樂、下載圖片等等,今天我將介紹一下利用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;
}
複製代碼