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中 扫描二维码/生成二维码详解 韩俊强的博客
最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...
- iOS中 本地通知/本地通知详解 韩俊强的博客
布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 Notification是智能手机应用编 ...
- iOS中 最新支付宝支付(AliPay) 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 现在的支付方式一般有三种, 支付宝, 微信, 网银. 个人觉得最简单易用的还是支付宝, 微信虽然看起来币支付宝要简单 ...
- iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...
- iOS中 最新微信支付/最全的微信支付教程详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 亲们, 首先让我们来看一下微信支付的流程吧. 1. 注册微信开放平台,创建应用获取appid,appSecret, ...
- iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 原文地址:http://blog.csdn.net/qq_31810357/article/details/5111 ...
- iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 整体布局如下: 程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang ...
- iOS中 百度地图详解 韩俊强的博文
需要准备工作按照下图引进类库 需要添加 添加的两个字符串为:NSLocationWhenInUseUsageDescription / NSLocationAlwaysUsageDescripti ...
随机推荐
- IOS和OSX事件传递机制
本文ios部分转载自: http://zhoon.github.io/ios/2015/04/12/ios-event.html iOS的事件有好几种:Touch Events(触摸事件).Motio ...
- 数组查找算法的C语言 实现-----线性查找和二分查找
线性查找 Linear Search 用户输入学生学号的成绩 二分查找 Binary Search 要求数据表是已经排好序的 程序存在小的瑕疵
- python类(class)中参数self的解释说明
python类(class)中参数self的简单解释 1.self只有在类的方法中才会有,其他函数或方法是不必带self的. 2.在调用时不必传入相应的参数.3.在类的方法中(如__init__),第 ...
- Helm 架构 - 每天5分钟玩转 Docker 容器技术(161)
在实践之前,我们先来看看 Helm 的架构. Helm 有两个重要的概念:chart 和 release. chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板.参数定 ...
- Node.js HTTPS
稳定性: 3 - 稳定 HTTPS 是基于 TLS/SSL 的 HTTP 协议.在 Node 里作为单独的模块来实现. 类: https.Server 这是 tls.Server 的子类,并且和 ht ...
- PHP 实例 - AJAX 实时搜索
AJAX Live Search 在下面的实例中,我们将演示一个实时的搜索,在您键入数据的同时即可得到搜索结果. 实时的搜索与传统的搜索相比,具有很多优势: 当键入数据时,就会显示出匹配的结果 当继续 ...
- Rails多路径调用相同方法原路返回的方法
有时候可能有多条path到达同一个method,此时,我们希望在该方法完成后自动转到之前进入的path中去,其实实现起来非常简单,只需要实现如下两个方法: def redirect_back_or(d ...
- Bootstrap3 表格-鼠标悬停
通过添加 .table-hover 类可以让 <tbody> 中的每一行对鼠标悬停状态作出响应. <table class="table table-hover" ...
- 自定义view实现阻尼效果的加载动画
效果: > 需要知识: 1. 二次贝塞尔曲线 2. 动画知识 3. 基础自定义view知识 先来解释下什么叫阻尼运动 阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减 ...
- Mac入门
Mac入门 桌面 windows桌面有图标罗列 Mac桌面有Dock 菜单栏 感觉上和Windows系统的底部菜单栏有点像,但是却略有不同,Mac的菜单栏默认在顶部 左侧的一些功能是固定不变的,跟随当 ...