iOS开发网络篇—使用ASI框架进行文件下载

说明:本文介绍iOS网络编程中经常用到的框架ASI,如何使用该框架进行文件的下载。

一、简单介绍

代码示例:

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 //下载服务器上的文件
19 [self download];
20 }
21
22 #pragma mark-下载文件
23 -(void)download
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 [request setDataReceivedBlock:^(NSData *data) {
30 NSLog(@"%d",data.length);
31 }];
32
33 //3.异步发送网络请求
34 [request startAsynchronous];
35 }
36
37 @end

代码说明:上面的代码从服务器上异步下载文件,每当接收到数据的时候就打印接收到的数据的长度。

打印结果如下:

使用ASI框架进行文件下载_ide

注意:在实际的开发中不能这样去下载文件,因为他不断的拼接文件数据的操作是在内存中进行的,如果所下载文件的数据较大,那么将会直接导致内存爆掉。

二、实际开发中的使用

代码示例(演示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 //下载服务器上的文件
19 [self download1];
20 }
21
22 #pragma mark-下载文件
23 //演示1
24 -(void)download
25 { //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 //演示2
39 -(void)download1
40 {
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框架进行文件下载_下载文件_02

代码说明:

在实际的开发中如果要使用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)viewDidLoad
11 {
12 [super viewDidLoad];
13 }
14
15 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
16 {
17 //下载服务器上的文件
18 [self download];
19 }
20
21 #pragma mark-下载文件
22 -(void)download
23 {
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 @end
10
11 @implementation YYViewController
12
13 - (void)viewDidLoad
14 {
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 *)event
23 {
24 //下载服务器上的文件
25 [self download];
26 }
27
28 #pragma mark-下载文件
29 -(void)download
30 {
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


显示效果:

使用ASI框架进行文件下载_下载文件_03

特别提示:

使用ASI框架进行文件下载_#import_04