布局如下:

基本拖拉属性:

#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中 断点下载详解 韩俊强的博客相关推荐

  1. iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客

    版权声明:本文为博主原创文章,未经博主允许不得转载. 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: [objc] view plaincopy //  ...

  2. iOS中 本地通知/本地通知详解 韩俊强的博客

    布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 Notification是智能手机应用编 ...

  3. iOS中 最新微信支付/最全的微信支付教程详解 韩俊强的博客

    亲们, 首先让我们来看一下微信支付的流程吧. 1. 注册微信开放平台,创建应用获取appid,appSecret,申请支付功能,申请成功之后会返回一些参数. 2. 下载微信支付sdk 3. 客户端请求 ...

  4. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graphics 和Quartz 2D的区别 quartz是一个通用的术语,用于描述在IOS和 ...

  5. iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客

    原文地址:http://blog.csdn.net/qq_31810357/article/details/51111702 前言:最近研究了一下语音识别,从百度语音识别到讯飞语音识别:首先说一下个人 ...

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

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

  7. iOS开发中的零碎知识点笔记 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 1.关联 objc_setAssociatedObject关联是指把两个对象相互关联起来,使得其中的一个对象作为另外 ...

  8. 2017年年终总结 韩俊强的博客

    前言 不知不觉,2017年又接近尾声了,又到了该写年终总结的时候了,往年这个时候都会熙熙攘攘,各大平台提早预热过年的气氛,而今年显得格外的平静,这可能正如我的现在的心境,波澜而不惊!因为今年整体过的只 ...

  9. iOS中 流媒体播放和下载 韩俊强的博客

    iOS中关于流媒体的简介:介于下载本地播放与实时流媒体之间的一种播放形式,下载本地播放必须全部将文件下载完成后才能播放,而渐进式下载不必等到全部下载完成后再播放,它可以一边下载一边播放,在完成播放内容 ...

  10. iOS中 Realm的学习与使用 韩俊强的博客

    iOS开发者交流QQ群:446310206  有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 所以重新做了一份 后面看到官网的中文文 ...

最新文章

  1. USEARCH11发布,新功能简介
  2. 基于 EventBridge 构建 SaaS 应用集成方案
  3. 华为鸿蒙官方翻译_鸿蒙系统官方译名来了?华为在欧盟注册Harmony商标
  4. 【GAN优化】如何选好正则项让你的GAN收敛
  5. BZOJ-2659-算不出的算式
  6. [资源分享] Github上八千Star的深度学习500问教程
  7. Educational Codeforces Round 53C(二分,思维|构造)
  8. App、小程序、H5,这三者该如何抉择?
  9. Python:pip下载库后导入Pycharm的方法
  10. 网关 配置内网DNS 服务器
  11. html小游戏社区,h5小游戏源码(h5养成社区源码)
  12. Programiz 中文系列教程·翻译完成
  13. VC2010编译源代码编辑控件scintilla
  14. 强制卸载 奇安信天擎
  15. 【RCV】接收单号丢失处理
  16. 2021-07-12 怎么将桌面图标变大变小
  17. 简单入门正则表达式 - 第三章 快速入门
  18. ERROR: Invalid subnet : invalid CIDR address: 解决办法
  19. unity 关于使用Rigidbody的Addforce但不起作用的一些可能原因以及解决方法
  20. 5个好用的设计素材网站

热门文章

  1. 【JZOJ3397】雨天的尾巴
  2. authorized_key 不生效。
  3. 计算机字长 按字编址,按字节编址与按字长编址区别及原理图解分析
  4. Thematic Contests -codeforce
  5. 修改linu主机名后 启动tomcat服务报Unable to set localhost. This prevents creation of a GUID. Cause was: qudaogua
  6. Between Us 3 人类的进化
  7. win11的控制面板在哪?
  8. 消费者购买决策行为研究模型
  9. 如何使用Pandas进行数据分析!最详细的数据分析教程!
  10. PowerVR 三十周年:回顾与展望