0.導(dǎo)入框架準(zhǔn)備工作
?1. 將AFNetworking3.0+框架程序拖拽進(jìn)項(xiàng)目
?2. 或使用Cocopod 導(dǎo)入AFNetworking3.0+
?3.??引入
#import "AFNetworking.h"
---->
1.UI準(zhǔn)備工作
A. 定義一個(gè)全局的 NSURLSessionDownloadTask:下載管理句柄
由其負(fù)責(zé)所有的網(wǎng)絡(luò)操作請(qǐng)求
@interfaceViewController ()
{
// 下載句柄
NSURLSessionDownloadTask *_downloadTask;
}
.h文件
#import
@interfaceViewController : UIViewController
// 下載文件顯示
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
// 下載進(jìn)度條顯示
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end
.m文件
@interfaceViewController ()
{
// 下載句柄
NSURLSessionDownloadTask *_downloadTask;
}
2.利用AFN實(shí)現(xiàn)文件下載操作細(xì)節(jié)
?(void)downFileFromServer{
//遠(yuǎn)程地址
NSURL *URL = [NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"];
//默認(rèn)配置
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//AFN3.0+基于封住URLSession的句柄
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//請(qǐng)求
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//下載Task操作
_downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
// @property int64_t totalUnitCount;???? 需要下載文件的總大小
// @property int64_t completedUnitCount; 當(dāng)前已經(jīng)下載的大小
// 給Progress添加監(jiān)聽 KVO
NSLog(@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
// 回到主隊(duì)列刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
// 設(shè)置進(jìn)度條的百分比
self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
});
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//- block的返回值, 要求返回一個(gè)URL, 返回的這個(gè)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) {
//設(shè)置下載完成操作
// filePath就是你下載文件的位置腕铸,你可以解壓姨拥,也可以直接拿來使用
NSString *imgFilePath = [filePath path];// 將NSURL轉(zhuǎn)成NSString
UIImage *img = [UIImage imageWithContentsOfFile:imgFilePath];
self.imageView.image = img;
}];
}
3.關(guān)于暫停和繼續(xù)
- (IBAction)stopDownloadBtnClick:(id)sender {
//暫停下載
[_downloadTask suspend];
}
- (IBAction)startDownloadBtnClick:(id)sender {
//開始下載
[_downloadTask resume];
}
4.檢測(cè)網(wǎng)絡(luò)狀態(tài)--優(yōu)化用戶體驗(yàn)
- (void)viewDidLoad {
[super viewDidLoad];
//網(wǎng)絡(luò)監(jiān)控句柄
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
//要監(jiān)控網(wǎng)絡(luò)連接狀態(tài),必須要先調(diào)用單例的startMonitoring方法
[manager startMonitoring];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//status:
//AFNetworkReachabilityStatusUnknown????????? = -1,? 未知
//AFNetworkReachabilityStatusNotReachable???? = 0,?? 未連接
//AFNetworkReachabilityStatusReachableViaWWAN = 1,?? 3G
//AFNetworkReachabilityStatusReachableViaWiFi = 2,?? 無線連接
NSLog(@"%d", status);
}];
//準(zhǔn)備從遠(yuǎn)程下載文件. -> 請(qǐng)點(diǎn)擊下面開始按鈕啟動(dòng)下載任務(wù)
[self downFileFromServer];
}