IOS成長之路-ASIHTTPRequest 斷點續傳

來源:互聯網
上載者:User

標籤:

1.匯入ASIHTTPRequest第三方類庫

:ASIHttpRequest類庫         完整代碼下載:下載

 

2.在 .h 檔案中

 

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2. #import "ASIHTTPRequest.h"  
  3. #import "ASINetworkQueue.h"  
  4. @interface DownLoadViewController : UIViewController<ASIHTTPRequestDelegate>  
  5. {  
  6.     UIProgressView *_progressView;  
  7. }  
  8.   
  9. @property(nonatomic, retain) UIProgressView *progressView;  
  10. @property(nonatomic, retain) ASINetworkQueue *asiQueue;  
  11. @property(nonatomic, retain) ASIHTTPRequest *asiHttpRequest;  
  12. @end  


3.在 .m 檔案中 實現這一過程

 

首先開啟隊列:

 

[cpp] view plaincopy
  1. _asiQueue=[[ASINetworkQueue alloc]init];//開啟隊列  
  2.     [_asiQueue reset];//nil  
  3.     _asiQueue.showAccurateProgress=YES;//進度  
  4.     [_asiQueue go];  


實現下載:

 

 

[cpp] view plaincopy
  1. NSURL *url = [NSURL URLWithString:@"請求地址"];  
  2.      
  3.    _asiHttpRequest=[ASIHTTPRequest requestWithURL:url];  
  4.    _asiHttpRequest.delegate=self;  
  5.    _asiHttpRequest.downloadProgressDelegate=self;//下載進度的代理,用於斷點續傳  
  6.     
  7.      
  8.     path = NSHomeDirectory();//該方法得到的是應用程式目錄的路徑  
  9.      
  10.    //目的路徑,設定一個目的路徑用來儲存下載下來的檔案  
  11.    NSString *savePath = [path stringByAppendingPathComponent:@"qgw.mp3"];  
  12.    /* 
  13.        臨時路徑: 
  14.            1.設定一個臨時路徑用來儲存下載過程中的檔案 
  15.            2.當下載完後會把這個檔案拷貝到目的路徑中,並刪除臨時路徑中的檔案 
  16.            3.斷點續傳:當設定斷點續傳的屬性為YES後,每次執行都會到臨時路徑中尋找要下載的檔案是否存在,下載的進度等等。。。然後就會在此基礎上繼續下載,從而實現續傳的效果 
  17.     設定臨時路徑在這個過程中是相當重要的。。。 
  18.     */  
  19.    NSString *temp = [path stringByAppendingPathComponent:@"temp"];  
  20.      
  21.    /* 
  22.        又在臨時路徑中添加了一個mp3格式的檔案,這就相當於設定了一個假的要下載的檔案,其實是不存在的,可以這麼理解:這裡提供了一個容器,下載的內容填充到了這個容器中。 
  23.        這個容器是必須要設定的,要不然它會不知道要下載到什麼裡面。。。 
  24.      
  25.        會有人說:問什麼不和上面的臨時路徑拚在一起,不是一樣麼:NSString *temp = [path stringByAppendingPathComponent:@"temp/qgw.mp3"]; 
  26.        這是不行的,因為你的臨時路徑必須要保證是正確的、是擁有的,所以在下面你要用NSFileManager來判斷是否存在這麼一個路徑,如果不存在就去建立, 
  27.        當你建立的時候會把qgw.mp3當作是一個檔案夾來建立的,所以每次斷點續傳的時候都會進入到qgw.mp3這個檔案夾中尋找,當然是找不到的(因為qwg.mp3就是) 
  28.        so,要分開來寫。。。 
  29.      
  30.     */  
  31.    NSString *tempPath = [temp stringByAppendingPathComponent:@"qgw.mp3"];  
  32.      
  33.    //建立檔案管理工具  
  34.    NSFileManager *fileManager = [NSFileManager defaultManager];  
  35.    //判斷temp檔案夾是否存在  
  36.    BOOL fileExists = [fileManager fileExistsAtPath:temp];  
  37.    if (!fileExists)  
  38.    {//如果不存在則建立,因為下載時,不會自動建立檔案夾  
  39.          
  40.        [fileManager createDirectoryAtPath:temp  
  41.               withIntermediateDirectories:YES  
  42.                                attributes:nil  
  43.                                     error:nil];  
  44.    }  
  45.      
  46.    [ _asiHttpRequest setDownloadDestinationPath:savePath ];//下載路徑  
  47.    [ _asiHttpRequest setTemporaryFileDownloadPath:tempPath ];//臨時路徑,一定要設定臨時路徑。。  
  48.      
  49.    _asiHttpRequest.allowResumeForFileDownloads = YES;//開啟斷點,是否要斷點續傳  
  50.      
  51.    //進度條  
  52.    _progressView.alpha = 1.0f;  
  53.    _progressView.progress=0;//賦值為0  
  54.      
  55.    [_asiQueue addOperation:_asiHttpRequest];//排入佇列  


得到下載的進度:

 

 

[cpp] view plaincopy
  1. //代理方法,得到下載進度  
  2. - (void)setProgress:(float)newProgress{  
  3.     [_progressView setProgress:newProgress];//賦給進度條  
  4. }  


停止下載:

 

 

[cpp] view plaincopy
  1. [_asiHttpRequest clearDelegatesAndCancel];//停掉  

IOS成長之路-ASIHTTPRequest 斷點續傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.