0.導入框架預備任務
1. 將A.networking3.0+框架順序拖拽進項目
2. 或運用Cocopod 導入A.networking3.0+
3. 引入
#import "A.networking.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.jb51.net/201701/yuanma/AFNetworking3.0_jb51.rar
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持本站。
【iOS應用AFNetworking3.0——完成文件斷點下載】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!