当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS利用AFNetworking3.0——实现文件断点下载

iOS利用AFNetworking3.0——实现文件断点下载

2019年07月24日  | 移动技术网移动技术  | 我要评论

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];

 

} 

源码:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网