ios開發---ASIHTTPRequest下載(支持斷點續傳)

   

      在工程中,咱們會經常遇到須要下載的程序,好比下載在線音樂、下載圖片等等,今天我將介紹一下利用ASIHTTPRequest的下載示例,支持斷點續傳,利用ASIHTTPRequest下載以及斷點續傳的原理,今天重點介紹如何實現,廢話少說,開始正文:

  1、建立網絡請求隊列

  首先,建立網絡請求隊列,以下:
 數組

  1.  ASINetworkQueue *que = [[ASINetworkQueue alloc] init];
    網絡

  2.   self.netWorkQueue = que;
    url

  3.   [que release];
    spa

  4.   [self.netWorkQueue reset];
    .net

  5.   [self.netWorkQueue setShowAccurateProgress:YES];
    代理

  6.   [self.netWorkQueue go];orm

複製代碼對象


  2、建立存放路徑

隊列

  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.   }

複製代碼


  3、發送下載請求

  這裏對下面幾個對象說明一下: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];

複製代碼



  3、暫停請求

  這裏的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.   }

複製代碼


  4、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.   }

複製代碼

相關文章
相關標籤/搜索