网络编程(三) 下载任务,支持断点续传
2016-01-28 23:48
519 查看
实现效果图:
gitHub地址:https://github.com/wl356485255/DownloadTaskDemo.git
注意点:1.下载的文件会保存在沙盒目录下的tmp文件夹内,这个文件夹专门存储一些临时文件,我们需要在下载完成后把文件移动到自己需要的文件夹内,并修改文件的扩展名
2.有部分链接使用下载时resume data = nil;这样断点续传功能就没有用了;
在storyboard里面添加button,progressView和显示进度的label,并进行连线
ViewController.m代码
gitHub地址:https://github.com/wl356485255/DownloadTaskDemo.git
注意点:1.下载的文件会保存在沙盒目录下的tmp文件夹内,这个文件夹专门存储一些临时文件,我们需要在下载完成后把文件移动到自己需要的文件夹内,并修改文件的扩展名
2.有部分链接使用下载时resume data = nil;这样断点续传功能就没有用了;
在storyboard里面添加button,progressView和显示进度的label,并进行连线
ViewController.m代码
#import "ViewController.h" @interface ViewController ()< NSURLSessionDownloadDelegate > { NSURLSessionDownloadTask *downTask; NSData *myResumeData; NSURLSession *session; NSString *fielName; } @property (weak, nonatomic) IBOutlet UIProgressView *progressView; @property (weak, nonatomic) IBOutlet UILabel *progressLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } //创建下载任务 - (IBAction)downloadAction:(id)sender { if (downTask) { return; } //构造需要下载的url链接 NSURL *url = [NSURL URLWithString:@"http://apk500.bce.baidu-mgame.com/game/883000/883227/20160127061354_oem_5004211.apk?r=1"]; //设置保存文件名为url的最后部分 fielName = [url lastPathComponent]; //设置session工作类型为默认 NSURLSessionConfiguration *sessionCf = [NSURLSessionConfiguration defaultSessionConfiguration]; //用sessionConfig配置session session = [NSURLSession sessionWithConfiguration:sessionCf delegate:self delegateQueue:[NSOperationQueue mainQueue]]; //设置任务类型 downTask = [session downloadTaskWithURL:url]; //发送任务请求 [downTask resume]; } //暂停下载任务 - (IBAction)pauseAction:(UIButton *)sender { if (downTask.state == NSURLSessionTaskStateRunning) { [downTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) { //If resume data cannot be created, the completion handler will be called with nil resumeData myResumeData = resumeData; downTask = nil; }]; } } //恢复下载任务 - (IBAction)resumeAction:(id)sender { if (myResumeData) { downTask = [session downloadTaskWithResumeData:myResumeData]; [downTask resume]; myResumeData = nil; } } //把下载来的文件移动到Documents文件夹下并修改扩展名 - (void)moveFileToDocuments:(NSURL *)location { NSFileManager *manager = [NSFileManager defaultManager]; NSString *newPath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fielName]]; fielName = nil; NSLog(@"%@",newPath); [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:newPath] error:nil]; } #pragma mark - NSURLSessionDownloadDelegate //在下载完成后调用 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSLog(@"%@",location); [self moveFileToDocuments:location]; } //进度条相关设置 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { float progress = (float)totalBytesWritten / totalBytesExpectedToWrite; self.progressView.progress = progress; self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress* 100]; }
相关文章推荐
- 2016-1-28 图解HTTP(03)
- Android开发请求网络方式详解
- HttpURLConnetion的使用
- 一个简单的Socket分包程序
- android网络交互
- Androidx学习笔记(31)--- android-smart-image-view查看网络图片
- Java NIO 网络编程
- HTTP协议里面request请求和response响应重要的头
- Centos6.5 网络配置
- 判断移动网络是否连接可用!
- 网络编程(基础)
- JAVA网络编程UDP案例
- JAVA网络编程UDP案例
- Win7环境下如何开启802.1X 无线网络认证
- Android Studio bug:Error:Could not run build action using Gradle distribution 'https://services.grad
- 神经网络中误差反向传播(back propagation)算法的工作原理
- 构造HTTP请求Header实现“伪造来源IP”
- AJAX中XMLHttpRequest对象详解(3)
- jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding
- 网络安全