iOS开发网络篇—使用ASI框架进行文件下载
說明:本文介紹iOS網(wǎng)絡(luò)編程中經(jīng)常用到的框架ASI,如何使用該框架進(jìn)行文件的下載。
一、簡(jiǎn)單介紹
代碼示例:
1 #import "YYViewController.h" 2 #import "ASIHTTPRequest.h" 3 4 @interface YYViewController () 5 6 7 @end 8 9 @implementation YYViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 } 15 16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 17 { 18 //下載服務(wù)器上的文件 19 [self download]; 20 } 21 22 #pragma mark-下載文件 23 -(void)download 24 { //1.創(chuàng)建請(qǐng)求對(duì)象 25 NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"]; 26 ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url]; 27 28 //2.添加請(qǐng)求參數(shù)(請(qǐng)求體中的參數(shù)) 29 [request setDataReceivedBlock:^(NSData *data) { 30 NSLog(@"%d",data.length); 31 }]; 32 33 //3.異步發(fā)送網(wǎng)絡(luò)請(qǐng)求 34 [request startAsynchronous]; 35 } 36 37 @end代碼說明:上面的代碼從服務(wù)器上異步下載文件,每當(dāng)接收到數(shù)據(jù)的時(shí)候就打印接收到的數(shù)據(jù)的長(zhǎng)度。
打印結(jié)果如下:
注意:在實(shí)際的開發(fā)中不能這樣去下載文件,因?yàn)樗粩嗟钠唇游募?shù)據(jù)的操作是在內(nèi)存中進(jìn)行的,如果所下載文件的數(shù)據(jù)較大,那么將會(huì)直接導(dǎo)致內(nèi)存爆掉。
二、實(shí)際開發(fā)中的使用
代碼示例(演示2):
1 #import "YYViewController.h" 2 #import "ASIHTTPRequest.h" 3 4 @interface YYViewController () 5 6 7 @end 8 9 @implementation YYViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 } 15 16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 17 { 18 //下載服務(wù)器上的文件 19 [self download1]; 20 } 21 22 #pragma mark-下載文件 23 //演示1 24 -(void)download 25 { //1.創(chuàng)建請(qǐng)求對(duì)象 26 NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"]; 27 ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url]; 28 29 //2.添加請(qǐng)求參數(shù)(請(qǐng)求體中的參數(shù)) 30 [request setDataReceivedBlock:^(NSData *data) { 31 NSLog(@"%d",data.length); 32 }]; 33 34 //3.異步發(fā)送網(wǎng)絡(luò)請(qǐng)求 35 [request startAsynchronous]; 36 } 37 38 //演示2 39 -(void)download1 40 { 41 //1.創(chuàng)建請(qǐng)求對(duì)象 42 NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"]; 43 ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url]; 44 45 //2.設(shè)置下載文件保存的路徑 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.發(fā)送網(wǎng)絡(luò)請(qǐng)求(異步) 52 [request startAsynchronous]; 53 54 //4.當(dāng)下載完成后通知 55 [request setCompletionBlock:^{ 56 NSLog(@"下載成功"); 57 }]; 58 } 59 60 @end下載成功:
代碼說明:
在實(shí)際的開發(fā)中如果要使用asi框架來下載服務(wù)器上的文件,只需要執(zhí)行下面簡(jiǎn)單的幾個(gè)步驟即可(參照上面的代碼)。
(1)創(chuàng)建請(qǐng)求對(duì)象;
(2)設(shè)置下載文件保存的路徑;
(3)發(fā)送下載文件的網(wǎng)絡(luò)請(qǐng)求(異步)。
按照上面的幾個(gè)步驟執(zhí)行,程序會(huì)自動(dòng)開啟異步線程,一點(diǎn)一點(diǎn)的把數(shù)據(jù)寫入到指定的文件路徑,而且不論是下載多大的文件都不會(huì)占用大量的內(nèi)存空間。
asi框架是基于底層的cfnoteworking的,性能很好。當(dāng)然也可以設(shè)置block,或者是監(jiān)聽下載的進(jìn)度。
下面介紹使用asi框架下載文件,如何監(jiān)聽下載的進(jìn)度。
設(shè)置下載代理,注意不是控制器代理。
代碼示例:
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)viewDidLoad 11 { 12 [super viewDidLoad]; 13 } 14 15 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 16 { 17 //下載服務(wù)器上的文件 18 [self download]; 19 } 20 21 #pragma mark-下載文件 22 -(void)download 23 { 24 //1.創(chuàng)建請(qǐng)求對(duì)象 25 NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"]; 26 ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url]; 27 28 //2.設(shè)置下載文件保存的路徑 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.設(shè)置下載進(jìn)度的代理 35 request.downloadProgressDelegate=self.progress; 36 37 //4.發(fā)送網(wǎng)絡(luò)請(qǐng)求(異步) 38 [request startAsynchronous]; 39 40 //5.下載完畢后通知 41 [request setCompletionBlock:^{ 42 NSLog(@"文件已經(jīng)下載完畢"); 43 }]; 44 } 45 46 #pragma mark-監(jiān)聽下載進(jìn)度的代理方法asi的文件下載還有一個(gè)屬性可以設(shè)置是否支持?jǐn)帱c(diǎn)下載。
設(shè)置支持?jǐn)帱c(diǎn)下載的代碼如下:
?request.allowResumeForFileDownloads=YES;
這樣的話,比如一個(gè)文件已經(jīng)下載了百分之30到程序的沙盒中,這個(gè)時(shí)候取消了下載。當(dāng)下一次點(diǎn)擊下載文件的時(shí)候,會(huì)接著下載剩余的百分之70并一點(diǎn)一點(diǎn)的寫入到沙盒中。
提示:取消下載的代碼為:
? ??[request clearDelegatesAndCancel];
?
三,結(jié)合一些進(jìn)度顯示的第三方框架使用
去code4app上面隨便下載一個(gè)顯示下載進(jìn)度的第三方框架,這里以DACircularProgressView為例子。
導(dǎo)入該框架必要的文件后,簡(jiǎn)單使用如下。
代碼示例:
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 @end 10 11 @implementation YYViewController 12 13 - (void)viewDidLoad 14 { 15 [super viewDidLoad]; 16 17 //設(shè)置基本的一些屬性 18 self.circularView.trackTintColor=[UIColor lightGrayColor]; 19 self.circularView.progressTintColor=[UIColor yellowColor]; 20 } 21 22 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 23 { 24 //下載服務(wù)器上的文件 25 [self download]; 26 } 27 28 #pragma mark-下載文件 29 -(void)download 30 { 31 //1.創(chuàng)建請(qǐng)求對(duì)象 32 NSURL *url=[NSURL URLWithString:@"http://127.0.0.1:8080/MJServer/resources/video.zip"]; 33 ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url]; 34 35 //2.設(shè)置下載文件保存的路徑 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.設(shè)置下載進(jìn)度的代理 42 request.downloadProgressDelegate=self.circularView; 43 44 //4.發(fā)送網(wǎng)絡(luò)請(qǐng)求(異步) 45 [request startAsynchronous]; 46 47 //5.設(shè)置支持?jǐn)帱c(diǎn)下載 48 request.allowResumeForFileDownloads=YES; 49 50 //5.下載完畢后通知 51 [request setCompletionBlock:^{ 52 NSLog(@"文件已經(jīng)下載完畢"); 53 }]; 54 } 55 56 #pragma mark-監(jiān)聽下載進(jìn)度的代理方法 57 @end顯示效果:
特別提示:
標(biāo)簽:?IOS開發(fā),?網(wǎng)絡(luò)篇總結(jié)
以上是生活随笔為你收集整理的iOS开发网络篇—使用ASI框架进行文件下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Alibaba Dubbo框架同步调用原
- 下一篇: git submodule获取子模块