国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS利用AFNetworking3.0――實現文件斷點下載

2020-07-26 02:57:21
字體:
來源:轉載
供稿:網友

0.導入框架準備工作  

1. 將AFNetworking3.0+框架程序拖拽進項目

2. 或使用Cocopod 導入AFNetworking3.0+

3.  引入

#import "AFNetworking.h"

1.UI準備工作  

A. 定義一個全局的 NSURLSessionDownloadTask:下載管理句柄

由其負責所有的網絡操作請求

 @interface ViewController (){ // 下載句柄 NSURLSessionDownloadTask *_downloadTask;} 

.h文件

#import <UIKit/UIKit.h>@interface ViewController : UIViewController// 下載文件顯示@property (weak, nonatomic) IBOutlet UIImageView *imageView;// 下載進度條顯示@property (weak, nonatomic) IBOutlet UIProgressView *progressView;@end

.m文件

@interface ViewController (){ // 下載句柄 NSURLSessionDownloadTask *_downloadTask;} 

2.利用AFN實現文件下載操作細節  

- (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.關于暫停和繼續  

- (IBAction)stopDownloadBtnClick:(id)sender { //暫停下載 [_downloadTask suspend];}- (IBAction)startDownloadBtnClick:(id)sender { //開始下載 [_downloadTask resume];} 

4.檢測網絡狀態--優化用戶體驗  

- (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://xiazai.VeVB.COm/201701/yuanma/AFNetworking3.0_jb51.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潍坊市| 河西区| 澄江县| 岑溪市| 东辽县| 綦江县| 松滋市| 美姑县| 朝阳县| 建德市| 鄂托克前旗| 叶城县| 前郭尔| 和静县| 湟中县| 吉安县| 本溪市| 历史| 江阴市| 阿克苏市| 古交市| 剑河县| 长治市| 青阳县| 普安县| 花莲市| 汪清县| 七台河市| 郴州市| 淄博市| 溆浦县| 怀宁县| 紫云| 连州市| 文登市| 宝山区| 睢宁县| 衡山县| 陕西省| 延庆县| 广南县|