版权声明:本文为博主原创文章,未经博主允许不得转载。

布局如下:

基本拖拉属性:

[objc] view plaincopy
  1. #import "ViewController.h"
  2. #import "AFNetworking.h"
  3. @interface ViewController ()
  4. @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
  5. @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
  6. @property (nonatomic, strong) AFHTTPRequestOperation *operation;
  7. @end
  8. @implementation ViewController

调用:

[objc] view plaincopy
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
  5. NSString *txtPath = [cachePath stringByAppendingPathComponent:@"mvTemp/mv.txt"];
  6. NSFileManager *fileManager = [NSFileManager defaultManager];
  7. if ([fileManager fileExistsAtPath:txtPath]) {
  8. self.progressView.progress = [[NSString stringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil] floatValue];
  9. self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", _progressView.progress * 100];
  10. } else {
  11. self.progressView.progress = 0;
  12. self.progressLabel.text = @"0%";
  13. }
  14. NSLog(@"%@", NSHomeDirectory());
  15. }

点击事件:

[objc] view plaincopy
  1. - (IBAction)startOrCancelDownLoad:(UIButton *)sender
  2. {
  3. if ([sender.currentTitle isEqualToString:@"开始下载"]) {
  4. [sender setTitle:@"暂停下载" forState:UIControlStateNormal];
  5. NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
  6. NSString *filePath = [cachePath stringByAppendingPathComponent:@"mv"];
  7. NSString *tempPath = [cachePath stringByAppendingPathComponent:@"mvTemp"];
  8. NSFileManager *fileManager = [NSFileManager defaultManager];
  9. if (![fileManager fileExistsAtPath:filePath]) {
  10. [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
  11. }
  12. if (![fileManager fileExistsAtPath:tempPath]) {
  13. [fileManager createDirectoryAtPath:tempPath withIntermediateDirectories:YES attributes:nil error:nil];
  14. }
  15. NSString *mp4TempPath = [tempPath stringByAppendingPathComponent:@"mv.temp"];
  16. NSString *txtTempPath = [tempPath stringByAppendingPathComponent:@"mv.txt"];
  17. NSString *mp4Path = [filePath stringByAppendingPathComponent:@"mv.mp4"];
  18. NSURL *url = [NSURL URLWithString:@"http://video.szzhangchu.com/1442395443772_5176326090.mp4"];
  19. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  20. unsigned long long downLoadBytes = 0;
  21. if ([fileManager fileExistsAtPath:mp4TempPath]) {
  22. downLoadBytes = [self fileSizeAtPath:mp4TempPath];
  23. NSString *range = [NSString stringWithFormat:@"bytes=%llu-", downLoadBytes];
  24. NSMutableURLRequest *mutableRequest = [request mutableCopy];
  25. [mutableRequest setValue:range forHTTPHeaderField:@"Range"];
  26. request = mutableRequest;
  27. }
  28. if (![fileManager fileExistsAtPath:mp4Path]) {
  29. self.operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  30. [self.operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:mp4TempPath append:YES]];
  31. __weak typeof(self) weakSelf = self;
  32. [_operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
  33. weakSelf.progressView.progress = (float)(totalBytesRead + downLoadBytes) / (float)(totalBytesExpectedToRead + downLoadBytes);
  34. weakSelf.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", weakSelf.progressView.progress * 100];
  35. NSString *progress = [NSString stringWithFormat:@"%.3f", weakSelf.progressView.progress];
  36. [progress writeToFile:txtTempPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  37. }];
  38. [_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  39. [fileManager moveItemAtPath:mp4TempPath toPath:mp4Path error:nil];
  40. [fileManager removeItemAtPath:txtTempPath error:nil];
  41. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  42. }];
  43. [_operation start];
  44. }
  45. } else {
  46. [sender setTitle:@"开始下载" forState:UIControlStateNormal];
  47. [self.operation cancel];
  48. _operation = nil;
  49. }
  50. }
[objc] view plaincopy
  1. - (unsigned long long)fileSizeAtPath:(NSString *)path
  2. {
  3. unsigned long long fileSize = 0;
  4. NSFileManager *fileManager = [NSFileManager defaultManager];
  5. if ([fileManager fileExistsAtPath:path]) {
  6. NSError *error = nil;
  7. NSDictionary *dict = [fileManager attributesOfItemAtPath:path error:&error];
  8. if (dict && !error) {
  9. fileSize = [dict fileSize];
  10. }
  11. }
  12. return fileSize;
  13. }

最终效果如下:

用到的第三方数据请求:AFNetworking,大家应该都有,这里不做介绍

关注博主微博每日更新技术:http://weibo.com/hanjunqiang

原文地址:http://blog.csdn.net/qq_31810357/article/details/49882769

iOS中 断点下载详解相关推荐

  1. [置顶] iOS中 支付宝钱包详解/第三方支付

    [置顶] iOS中 支付宝钱包详解/第三方支付 韩俊强的博客 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 一.在app中成功完成支付宝支付的过程 1.申请支付 ...

  2. iOS中ImageIO框架详解与应用分析

    2019独角兽企业重金招聘Python工程师标准>>> iOS中ImageIO框架详解与应用分析 一.引言 ImageIO框架提供了读取与写入图片数据的基本方法,使用它可以直接获取到 ...

  3. iOS中 支付宝钱包详解/第三方支付 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址 ...

  4. iOS中 支付宝钱包详解

    一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址: https://b.alipay.com/order/productDetail.htm?productId=201411030 ...

  5. iOS中 支付宝钱包详解/第三方支付

    版权声明:本文为博主原创文章,未经博主允许不得转载. 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包. ...

  6. iOS中block的详解weakSelf、strongSelf-转自唐巧

    1 我们知道,在使用 block 的时候,为了避免产生循环引用,通常需要使用 weakSelf 与 strongSelf,写下面这样的代码: __weak typeof(self) weakSelf ...

  7. IOS 中 pch 文件详解

    在 Xcode6 之前,创建一个新工程会在 Supporting files 文件夹下面自动创建一个"工程名-Prefix.pch"文件,也是一个头文件,pch 头文件的内容能被项 ...

  8. iOS中UIActionSheet使用详解

    一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)de ...

  9. iOS中 百度地图详解 韩俊强的博文

    需要准备工作按照下图引进类库 需要添加 添加的两个字符串为:NSLocationWhenInUseUsageDescription  /  NSLocationAlwaysUsageDescripti ...

  10. iOS 原生级别后台下载详解

    怎样才能并发地下载一堆文件,并且能够在后台全部下载完成后再执行其他操作? 当然,这个问题其实很简单,解决方案也有很多.但我第一时间想到的是,目前是否存一个非常权威,非常流行.稳定可靠,并且是用 Swi ...

最新文章

  1. 【Python】身体质量指数BMI
  2. 数字签名 那些密码技术_密码学中的数字签名
  3. 平均每个员工2000万!苹果为啥买下这家刚成立3年的AI创业公司?
  4. 利用CEF山寨一个翻译器
  5. 文件操作03 - 零基础入门学习C语言62
  6. tensorflow中如何进行可视化和减轻过拟合(转)
  7. Postman 汉化包(设置中文)
  8. 北斗时间周和GPS时间周计算,JAVA为例
  9. matlab折线参数,matlab画含参数曲线族
  10. hi3519开发流程
  11. pandas按照多列排序-ascending
  12. 计算机now函数,玩转NOW函数 日期时间随心变
  13. Istio学习笔记-熔断实验
  14. MES1.0实现分析 -制造业数字化探讨(1)
  15. ClickHouse字段分组取TOP N
  16. 重庆云阳2021云中高考成绩查询,重庆云阳中学2021年招生简章
  17. java shiro_shiro(java安全框架)
  18. msi2lmp不能用,mpi不能连接主机,解决办法看这里
  19. 解决URP资源的材质成洋红色问题
  20. 全攻略:实现pynq-z2条形码识别

热门文章

  1. There appears to be trouble with your network connection
  2. python根据经纬度画热力图_【python】使用python按照一定格式批量输出,地图热力图经纬度格式生成器...
  3. WPF MessageBox 添加确认取消按钮 并判断
  4. 计算机打字皮肤怎么退出,使用搜狗输入法电脑版中设置快捷键更换皮肤的方法...
  5. 传奇服务器 地图文件,[技术贴]地图配置文件
  6. 有赞 CTO 崔玉松:我想打造出中国最好的技术团队
  7. Photoshop脚本 镜头光晕滤镜的使用
  8. git语法大全(值得收藏)
  9. 概率逻辑程序设计学习 一.预备知识
  10. 51单片机程序存储器扩展