0.導入框架準備工作
1. 將afnetworking3.0+框架程序拖拽進項目
2. 或使用cocopod 導入afnetworking3.0+
3. 引入
1
|
#import "afnetworking.h" |
1.ui準備工作
a. 定義一個全局的 nsurlsessiondownloadtask:下載管理句柄
由其負責所有的網絡操作請求
1
2
3
4
5
6
7
8
9
|
@interface viewcontroller () { // 下載句柄 nsurlsessiondownloadtask *_downloadtask; } |
.h文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller // 下載文件顯示 @property (weak, nonatomic) iboutlet uiimageview *imageview; // 下載進度條顯示 @property (weak, nonatomic) iboutlet uiprogressview *progressview; @end |
.m文件
1
2
3
4
5
6
7
8
9
|
@interface viewcontroller () { // 下載句柄 nsurlsessiondownloadtask *_downloadtask; } |
2.利用afn實現文件下載操作細節
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
- ( void )downfilefromserver{ //遠程地址 nsurl *url = [nsurl urlwithstring:@ "http://www.baidu.com/img/bdlogo.png" ]; //默認配置 nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; //afn3.0+基于封住urlsession的句柄 afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; //請求 nsurlrequest *request = [nsurlrequest requestwithurl:url]; //下載task操作 _downloadtask = [manager downloadtaskwithrequest:request progress:^(nsprogress * _nonnull downloadprogress) { // @property int64_t totalunitcount; 需要下載文件的總大小 // @property int64_t completedunitcount; 當前已經下載的大小 // 給progress添加監聽 kvo nslog(@ "%f" ,1.0 * downloadprogress.completedunitcount / downloadprogress.totalunitcount); // 回到主隊列刷新ui dispatch_async(dispatch_get_main_queue(), ^{ // 設置進度條的百分比 self.progressview.progress = 1.0 * downloadprogress.completedunitcount / downloadprogress.totalunitcount; }); } destination:^nsurl * _nonnull(nsurl * _nonnull targetpath, nsurlresponse * _nonnull response) { //- block的返回值, 要求返回一個url, 返回的這個url就是文件的位置的路徑 nsstring *cachespath = [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) lastobject]; nsstring *path = [cachespath stringbyappendingpathcomponent:response.suggestedfilename]; return [nsurl fileurlwithpath:path]; } completionhandler:^(nsurlresponse * _nonnull response, nsurl * _nullable filepath, nserror * _nullable error) { //設置下載完成操作 // filepath就是你下載文件的位置,你可以解壓,也可以直接拿來使用 nsstring *imgfilepath = [filepath path]; // 將nsurl轉成nsstring uiimage *img = [uiimage imagewithcontentsoffile:imgfilepath]; self.imageview.image = img; }]; } |
3.關于暫停和繼續
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
- (ibaction)stopdownloadbtnclick:(id)sender { //暫停下載 [_downloadtask suspend]; } - (ibaction)startdownloadbtnclick:(id)sender { //開始下載 [_downloadtask resume]; } |
4.檢測網絡狀態--優化用戶體驗
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
- ( void )viewdidload { [super viewdidload]; //網絡監控句柄 afnetworkreachabilitymanager *manager = [afnetworkreachabilitymanager sharedmanager]; //要監控網絡連接狀態,必須要先調用單例的startmonitoring方法 [manager startmonitoring]; [manager setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) { //status: //afnetworkreachabilitystatusunknown = -1, 未知 //afnetworkreachabilitystatusnotreachable = 0, 未連接 //afnetworkreachabilitystatusreachableviawwan = 1, 3g //afnetworkreachabilitystatusreachableviawifi = 2, 無線連接 nslog(@ "%d" , status); }]; //準備從遠程下載文件. -> 請點擊下面開始按鈕啟動下載任務 [self downfilefromserver]; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/qingche/p/5362592.html