网络---大文件的下载(NSURLSession)
2016-02-25 22:48
387 查看
小文件的下载相对比较简单,但往往大文件的下载会比较常见,也相对来说比较复杂
利用NSUrlSession
注意:使用此方法并不能查看下载进度,如果想要实现下载进度,需使用下面方法
#pragma mark --- NSURLSessionDownloadDelegate 代理方法
利用NSUrlSession
NSURLSession * session = [NSURLSession sharedSession]; NSURL * url = [NSURL URLWithString:@"下载地址"]; [[session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { // cache路径(保存在Library下的caches中) NSString * caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // 拼接文件路径 NSString * filePath = [caches stringByAppendingPathComponent:response.suggestedFilename]; // 下载完成后将文件移动到 filePath 上 NSFileManager * fileMgr = [NSFileManager defaultManager]; [fileMgr moveItemAtPath:location.path toPath:filePath error:nil]; }] resume];
注意:使用此方法并不能查看下载进度,如果想要实现下载进度,需使用下面方法
NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration]; // 通过设置代理的方法来实现 NSURLSession * session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; NSURL * url = [NSURL URLWithString:@"下载地址"]; [[session downloadTaskWithURL:url] resume];
#pragma mark --- NSURLSessionDownloadDelegate 代理方法
/** * 下载完毕后调用 * * @param location 临时文件的路径(下载好的文件) */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { // location : 临时文件的路径(下载好的文件) NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致 NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename]; // 将临时文件剪切或者复制Caches文件夹 NSFileManager *mgr = [NSFileManager defaultManager]; // AtPath : 剪切前的文件路径 // ToPath : 剪切后的文件路径 [mgr moveItemAtPath:location.path toPath:file error:nil]; } /** * 恢复下载时调用 */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes { } /** * 每当下载完(写完)一部分时就会调用(可能会被调用多次) * * @param bytesWritten 这次调用写了多少 * @param totalBytesWritten 累计写了多少长度到沙盒中了 * @param totalBytesExpectedToWrite 文件的总长度 */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { double progress = (double)totalBytesWritten / totalBytesExpectedToWrite; NSLog(@"下载进度---%f", progress); }
相关文章推荐
- A/V data用TCP来接收(rtp overrtsp),可以收到数据,但解析不对。
- TCP三次握手/四次握手
- 【深度学习介绍系列之二】——深度强化学习:卷积神经网络
- ACM_模板_网络流
- 网络---小文件的下载
- 连接网络设备的交叉线和直通线的区别
- 有上下界的网络流问题
- iOS之HTTP网络编程
- TCP/IP 三次握手,温故知新
- liunx centos5.8 下配置apache+svn的http访问
- 贝叶斯网络--概率推理
- poj 3281 Dining 拆点网络流
- 李望 原创作品转载请注明出处 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-10000290
- HDU1532 - Drainage Ditches(网络流)
- 网络编程_UDP编程原理_发送类型_cs与bs区别JAVA187-188
- HTTP协议
- Android开发之旅:组件生命周期(一)出处:http://www.cnblogs.com/skynet/
- HDU3549 - Flow Problem(模板网络流 + Ford-Fulkerson算法)
- poj 1459 Power Network 多源多汇网络流
- 人工神经网络训练样本的选择方法