0.导入框架准备工作
?1. 将AFNetworking3.0+框架程序拖拽进项目
?2. 或使用Cocopod 导入AFNetworking3.0+
?3. 引入
#import "AFNetworking.h"
---->
1.UI准备工作
A. 定义一个全局的 NSURLSessionDownloadTask:下载管理句柄
由其负责所有的网络操作请求
1
@interface
ViewController ()
2
{
3
//
下载句柄
4 NSURLSessionDownloadTask *_downloadTask;
5 }
1
.h文件
2
#import <UIKit/UIKit.h>
3 4@interface ViewController : UIViewController
5// 下载文件显示 6 7 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
8// 下载进度条显示 910 @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
11@end1213.m文件
14@interface ViewController ()
15{
16// 下载句柄17 NSURLSessionDownloadTask *_downloadTask;
18 }
2.利用AFN实现文件下载操作细节
1 - (void)downFileFromServer{
2 3//远程地址 4 NSURL *URL = [NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"];
5//默认配置 6 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
7 8//AFN3.0+基于封住URLSession的句柄 9 AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
1011//请求12 NSURLRequest *request = [NSURLRequest requestWithURL:URL];
1314//下载Task操作15 _downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
1617// @property int64_t totalUnitCount; 需要下载文件的总大小
18// @property int64_t completedUnitCount; 当前已经下载的大小
1920// 给Progress添加监听 KVO21 NSLog(@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
22// 回到主队列刷新UI23 dispatch_async(dispatch_get_main_queue(), ^{
24// 设置进度条的百分比2526 self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
27 });
2829 } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
3031//- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径3233 NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
34 NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
35return [NSURL fileURLWithPath:path];
3637 } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
38//设置下载完成操作
39// filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用4041 NSString *imgFilePath = [filePath path];// 将NSURL转成NSString42 UIImage *img = [UIImage imageWithContentsOfFile:imgFilePath];
43 self.imageView.image = img;
4445 }];
46 }
3.关于暂停和继续
1 - (IBAction)stopDownloadBtnClick:(id)sender {
2//暂停下载3 [_downloadTask suspend];
4}
5 - (IBAction)startDownloadBtnClick:(id)sender {
6//开始下载7 [_downloadTask resume];
8 }
4.检测网络状态--优化用户体验
1 - (void)viewDidLoad {
2 [super viewDidLoad];
3 4//网络监控句柄 5 AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
6 7//要监控网络连接状态,必须要先调用单例的startMonitoring方法 8 [manager startMonitoring];
910 [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
11//status:
12//AFNetworkReachabilityStatusUnknown = -1, 未知
13//AFNetworkReachabilityStatusNotReachable = 0, 未连接
14//AFNetworkReachabilityStatusReachableViaWWAN = 1, 3G
15//AFNetworkReachabilityStatusReachableViaWiFi = 2, 无线连接16 NSLog(@"%d", status);
17 }];
1819//准备从远程下载文件. -> 请点击下面开始按钮启动下载任务20 [self downFileFromServer];
2122 }
转自:http://www.cnblogs.com/qingche/p/5362592.html
原文:http://www.cnblogs.com/lhw91/p/5687716.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!