iOS中 断点下载详解
版权声明:本文为博主原创文章,未经博主允许不得转载。
布局如下:
基本拖拉属性:
- #import "ViewController.h"
- #import "AFNetworking.h"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
- @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
- @property (nonatomic, strong) AFHTTPRequestOperation *operation;
- @end
- @implementation ViewController
调用:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
- NSString *txtPath = [cachePath stringByAppendingPathComponent:@"mvTemp/mv.txt"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:txtPath]) {
- self.progressView.progress = [[NSString stringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil] floatValue];
- self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", _progressView.progress * 100];
- } else {
- self.progressView.progress = 0;
- self.progressLabel.text = @"0%";
- }
- NSLog(@"%@", NSHomeDirectory());
- }
点击事件:
- - (IBAction)startOrCancelDownLoad:(UIButton *)sender
- {
- if ([sender.currentTitle isEqualToString:@"开始下载"]) {
- [sender setTitle:@"暂停下载" forState:UIControlStateNormal];
- NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
- NSString *filePath = [cachePath stringByAppendingPathComponent:@"mv"];
- NSString *tempPath = [cachePath stringByAppendingPathComponent:@"mvTemp"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:filePath]) {
- [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
- }
- if (![fileManager fileExistsAtPath:tempPath]) {
- [fileManager createDirectoryAtPath:tempPath withIntermediateDirectories:YES attributes:nil error:nil];
- }
- NSString *mp4TempPath = [tempPath stringByAppendingPathComponent:@"mv.temp"];
- NSString *txtTempPath = [tempPath stringByAppendingPathComponent:@"mv.txt"];
- NSString *mp4Path = [filePath stringByAppendingPathComponent:@"mv.mp4"];
- NSURL *url = [NSURL URLWithString:@"http://video.szzhangchu.com/1442395443772_5176326090.mp4"];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- unsigned long long downLoadBytes = 0;
- if ([fileManager fileExistsAtPath:mp4TempPath]) {
- downLoadBytes = [self fileSizeAtPath:mp4TempPath];
- NSString *range = [NSString stringWithFormat:@"bytes=%llu-", downLoadBytes];
- NSMutableURLRequest *mutableRequest = [request mutableCopy];
- [mutableRequest setValue:range forHTTPHeaderField:@"Range"];
- request = mutableRequest;
- }
- if (![fileManager fileExistsAtPath:mp4Path]) {
- self.operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- [self.operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:mp4TempPath append:YES]];
- __weak typeof(self) weakSelf = self;
- [_operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
- weakSelf.progressView.progress = (float)(totalBytesRead + downLoadBytes) / (float)(totalBytesExpectedToRead + downLoadBytes);
- weakSelf.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", weakSelf.progressView.progress * 100];
- NSString *progress = [NSString stringWithFormat:@"%.3f", weakSelf.progressView.progress];
- [progress writeToFile:txtTempPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- }];
- [_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
- [fileManager moveItemAtPath:mp4TempPath toPath:mp4Path error:nil];
- [fileManager removeItemAtPath:txtTempPath error:nil];
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- }];
- [_operation start];
- }
- } else {
- [sender setTitle:@"开始下载" forState:UIControlStateNormal];
- [self.operation cancel];
- _operation = nil;
- }
- }
- - (unsigned long long)fileSizeAtPath:(NSString *)path
- {
- unsigned long long fileSize = 0;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:path]) {
- NSError *error = nil;
- NSDictionary *dict = [fileManager attributesOfItemAtPath:path error:&error];
- if (dict && !error) {
- fileSize = [dict fileSize];
- }
- }
- return fileSize;
- }
最终效果如下:
用到的第三方数据请求:AFNetworking,大家应该都有,这里不做介绍
关注博主微博每日更新技术:http://weibo.com/hanjunqiang
iOS中 断点下载详解相关推荐
- [置顶] iOS中 支付宝钱包详解/第三方支付
[置顶] iOS中 支付宝钱包详解/第三方支付 韩俊强的博客 每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 一.在app中成功完成支付宝支付的过程 1.申请支付 ...
- iOS中ImageIO框架详解与应用分析
2019独角兽企业重金招聘Python工程师标准>>> iOS中ImageIO框架详解与应用分析 一.引言 ImageIO框架提供了读取与写入图片数据的基本方法,使用它可以直接获取到 ...
- iOS中 支付宝钱包详解/第三方支付 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! iOS开发者交流QQ群: 446310206 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址 ...
- iOS中 支付宝钱包详解
一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址: https://b.alipay.com/order/productDetail.htm?productId=201411030 ...
- iOS中 支付宝钱包详解/第三方支付
版权声明:本文为博主原创文章,未经博主允许不得转载. 每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包. ...
- iOS中block的详解weakSelf、strongSelf-转自唐巧
1 我们知道,在使用 block 的时候,为了避免产生循环引用,通常需要使用 weakSelf 与 strongSelf,写下面这样的代码: __weak typeof(self) weakSelf ...
- IOS 中 pch 文件详解
在 Xcode6 之前,创建一个新工程会在 Supporting files 文件夹下面自动创建一个"工程名-Prefix.pch"文件,也是一个头文件,pch 头文件的内容能被项 ...
- iOS中UIActionSheet使用详解
一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)de ...
- iOS中 百度地图详解 韩俊强的博文
需要准备工作按照下图引进类库 需要添加 添加的两个字符串为:NSLocationWhenInUseUsageDescription / NSLocationAlwaysUsageDescripti ...
- iOS 原生级别后台下载详解
怎样才能并发地下载一堆文件,并且能够在后台全部下载完成后再执行其他操作? 当然,这个问题其实很简单,解决方案也有很多.但我第一时间想到的是,目前是否存一个非常权威,非常流行.稳定可靠,并且是用 Swi ...
最新文章
- 【Python】身体质量指数BMI
- 数字签名 那些密码技术_密码学中的数字签名
- 平均每个员工2000万!苹果为啥买下这家刚成立3年的AI创业公司?
- 利用CEF山寨一个翻译器
- 文件操作03 - 零基础入门学习C语言62
- tensorflow中如何进行可视化和减轻过拟合(转)
- Postman 汉化包(设置中文)
- 北斗时间周和GPS时间周计算,JAVA为例
- matlab折线参数,matlab画含参数曲线族
- hi3519开发流程
- pandas按照多列排序-ascending
- 计算机now函数,玩转NOW函数 日期时间随心变
- Istio学习笔记-熔断实验
- MES1.0实现分析 -制造业数字化探讨(1)
- ClickHouse字段分组取TOP N
- 重庆云阳2021云中高考成绩查询,重庆云阳中学2021年招生简章
- java shiro_shiro(java安全框架)
- msi2lmp不能用,mpi不能连接主机,解决办法看这里
- 解决URP资源的材质成洋红色问题
- 全攻略:实现pynq-z2条形码识别
热门文章
- There appears to be trouble with your network connection
- python根据经纬度画热力图_【python】使用python按照一定格式批量输出,地图热力图经纬度格式生成器...
- WPF MessageBox 添加确认取消按钮 并判断
- 计算机打字皮肤怎么退出,使用搜狗输入法电脑版中设置快捷键更换皮肤的方法...
- 传奇服务器 地图文件,[技术贴]地图配置文件
- 有赞 CTO 崔玉松:我想打造出中国最好的技术团队
- Photoshop脚本 镜头光晕滤镜的使用
- git语法大全(值得收藏)
- 概率逻辑程序设计学习 一.预备知识
- 51单片机程序存储器扩展