标签:
---->
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 // 下载进度条显示 9 10 @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 11 @end 12 13 .m文件 14 @interface ViewController () 15 { 16 // 下载句柄 17 NSURLSessionDownloadTask *_downloadTask; 18 }
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]; 10 11 //请求 12 NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 13 14 //下载Task操作 15 _downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { 16 17 // @property int64_t totalUnitCount; 需要下载文件的总大小 18 // @property int64_t completedUnitCount; 当前已经下载的大小 19 20 // 给Progress添加监听 KVO 21 NSLog(@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount); 22 // 回到主队列刷新UI 23 dispatch_async(dispatch_get_main_queue(), ^{ 24 // 设置进度条的百分比 25 26 self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount; 27 }); 28 29 } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { 30 31 //- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径 32 33 NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 34 NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename]; 35 return [NSURL fileURLWithPath:path]; 36 37 } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { 38 //设置下载完成操作 39 // filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用 40 41 NSString *imgFilePath = [filePath path];// 将NSURL转成NSString 42 UIImage *img = [UIImage imageWithContentsOfFile:imgFilePath]; 43 self.imageView.image = img; 44 45 }]; 46 }
1 - (IBAction)stopDownloadBtnClick:(id)sender { 2 //暂停下载 3 [_downloadTask suspend]; 4 } 5 - (IBAction)startDownloadBtnClick:(id)sender { 6 //开始下载 7 [_downloadTask resume]; 8 }
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //网络监控句柄 5 AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager]; 6 7 //要监控网络连接状态,必须要先调用单例的startMonitoring方法 8 [manager startMonitoring]; 9 10 [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 11 //status: 12 //AFNetworkReachabilityStatusUnknown = -1, 未知 13 //AFNetworkReachabilityStatusNotReachable = 0, 未连接 14 //AFNetworkReachabilityStatusReachableViaWWAN = 1, 3G 15 //AFNetworkReachabilityStatusReachableViaWiFi = 2, 无线连接 16 NSLog(@"%d", status); 17 }]; 18 19 //准备从远程下载文件. -> 请点击下面开始按钮启动下载任务 20 [self downFileFromServer]; 21 22 }
转自:http://www.cnblogs.com/qingche/p/5362592.html
iOS- 利用AFNetworking3.0+(最新AFN) - 实现文件断点下载
标签:
原文地址:http://www.cnblogs.com/lhw91/p/5687716.html