iOS開發網路篇—使用ASI架構進行檔案下載

來源:互聯網
上載者:User

標籤:

iOS開發網路篇—使用ASI架構進行檔案下載

說明:本文介紹iOS網路編程中經常用到的架構ASI,如何使用該架構進行檔案的下載。

一、簡單介紹

程式碼範例:

 1 #import "YYViewController.h" 2 #import "ASIHTTPRequest.h" 3  4 @interface YYViewController () 5  6  7 @end 8  9 @implementation YYViewController10 11 - (void)viewDidLoad12 {13     [super viewDidLoad];14 }15 16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event17 {18     //下載伺服器上的檔案19     [self download];20 }21 22 #pragma mark-下載檔案23 -(void)download24 {    //1.建立請求對象25     NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"];26     ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];27     28     //2.添加請求參數(請求體中的參數)29     [request setDataReceivedBlock:^(NSData *data) {30         NSLog(@"%d",data.length);31     }];32     33     //3.非同步發送網路請求34     [request startAsynchronous];35 }36 37 @end

代碼說明:上面的代碼從伺服器上非同步下載檔案,每當接收到資料的時候就列印接收到的資料的長度。

列印結果如下:

注意:在實際的開發中不能這樣去下載檔案,因為他不斷的拼接檔案資料的操作是在記憶體中進行的,如果所下載檔案的資料較大,那麼將會直接導致記憶體爆掉。

二、實際開發中的使用

程式碼範例(示範2):

 1 #import "YYViewController.h" 2 #import "ASIHTTPRequest.h" 3  4 @interface YYViewController () 5  6  7 @end 8  9 @implementation YYViewController10 11 - (void)viewDidLoad12 {13     [super viewDidLoad];14 }15 16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event17 {18     //下載伺服器上的檔案19     [self download1];20 }21 22 #pragma mark-下載檔案23 //示範124 -(void)download25 {    //1.建立請求對象26     NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"];27     ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];28     29     //2.添加請求參數(請求體中的參數)30     [request setDataReceivedBlock:^(NSData *data) {31         NSLog(@"%d",data.length);32     }];33     34     //3.非同步發送網路請求35     [request startAsynchronous];36 }37 38 //示範239 -(void)download140 {41     //1.建立請求對象42     NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"];43     ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];44     45     //2.設定下載檔案儲存的路徑46     NSString *cachepath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];47     NSString *filename=[cachepath stringByAppendingPathComponent:@"video.zip"];48     request.downloadDestinationPath=filename;49     NSLog(@"%@",filename);50     51     //3.發送網路請求(非同步)52     [request startAsynchronous];53     54     //4.當下載完成後通知55     [request setCompletionBlock:^{56         NSLog(@"下載成功");57     }];58 }59 60 @end

下載成功:

代碼說明:

在實際的開發中如果要使用asi架構來下載伺服器上的檔案,只需要執行下面簡單的幾個步驟即可(參照上面的代碼)。

(1)建立請求對象;

(2)設定下載檔案儲存的路徑;

(3)發送下載檔案的網路請求(非同步)。

按照上面的幾個步驟執行,程式會自動開啟非同步線程,一點一點的把資料寫入到指定的檔案路徑,而且不論是下載多大的檔案都不會佔用大量的記憶體空間。

asi架構是基於底層的cfnoteworking的,效能很好。當然也可以設定block,或者是監聽下載的進度。

下面介紹使用asi架構下載檔案,如何監聽下載的進度。

設定下載代理,注意不是控制器代理。

程式碼範例:

 1 #import "YYViewController.h" 2 #import "ASIHTTPRequest.h" 3  4 @interface YYViewController ()<ASIProgressDelegate> 5 @property (weak, nonatomic) IBOutlet UIProgressView *progress; 6 @end 7  8 @implementation YYViewController 9 10 - (void)viewDidLoad11 {12     [super viewDidLoad];13 }14 15 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event16 {17     //下載伺服器上的檔案18     [self download];19 }20 21 #pragma mark-下載檔案22 -(void)download23 {24     //1.建立請求對象25     NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"];26     ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];27     28     //2.設定下載檔案儲存的路徑29     NSString *cachepath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];30     NSString *filename=[cachepath stringByAppendingPathComponent:@"video.zip"];31     request.downloadDestinationPath=filename;32     NSLog(@"%@",filename);33     34     //3.設定下載進度的代理35     request.downloadProgressDelegate=self.progress;36     37     //4.發送網路請求(非同步)38     [request startAsynchronous];39     40     //5.下載完畢後通知41     [request setCompletionBlock:^{42         NSLog(@"檔案已經下載完畢");43     }];44 }45 46 #pragma mark-監聽下載進度的代理方法

asi的檔案下載還有一個屬性可以設定是否支援斷點下載。

設定支援斷點下載的代碼如下:

 request.allowResumeForFileDownloads=YES;

這樣的話,比如一個檔案已經下載了百分之30到程式的沙箱中,這個時候取消了下載。當下一次點擊下載檔案的時候,會接著下載剩餘的百分之70並一點一點的寫入到沙箱中。

提示:取消下載的代碼為:

    [request clearDelegatesAndCancel];

 

三,結合一些進度顯示的第三方架構使用

去code4app上面隨便下載一個顯示下載進度的第三方架構,這裡以DACircularProgressView為例子。

匯入該架構必要的檔案後,簡單使用如下。

程式碼範例:

 1 #import "YYViewController.h" 2 #import "ASIHTTPRequest.h" 3 #import "DACircularProgressView.h" 4  5 @interface YYViewController ()<ASIProgressDelegate> 6  7 @property (weak, nonatomic) IBOutlet DACircularProgressView *circularView; 8 @property (weak, nonatomic) IBOutlet UIProgressView *progress; 9 @end10 11 @implementation YYViewController12 13 - (void)viewDidLoad14 {15     [super viewDidLoad];16     17     //設定基本的一些屬性18     self.circularView.trackTintColor=[UIColor lightGrayColor];19     self.circularView.progressTintColor=[UIColor yellowColor];20 }21 22 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event23 {24     //下載伺服器上的檔案25     [self download];26 }27 28 #pragma mark-下載檔案29 -(void)download30 {31     //1.建立請求對象32     NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"];33     ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];34     35     //2.設定下載檔案儲存的路徑36     NSString *cachepath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];37     NSString *filename=[cachepath stringByAppendingPathComponent:@"video.zip"];38     request.downloadDestinationPath=filename;39     NSLog(@"%@",filename);40     41     //3.設定下載進度的代理42     request.downloadProgressDelegate=self.circularView;43     44     //4.發送網路請求(非同步)45     [request startAsynchronous];46     47     //5.設定支援斷點下載48     request.allowResumeForFileDownloads=YES;49     50     //5.下載完畢後通知51     [request setCompletionBlock:^{52         NSLog(@"檔案已經下載完畢");53     }];54 }55 56 #pragma mark-監聽下載進度的代理方法57 @end

顯示效果:

特別提示:

iOS開發網路篇—使用ASI架構進行檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.