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 ...
随机推荐
- SCNN车道线检测--(SCNN)Spatial As Deep: Spatial CNN for Traffic Scene Understanding(论文解读)
Spatial As Deep: Spatial CNN for Traffic Scene Understanding 收录:AAAI2018 (AAAI Conference on Artific ...
- jquery easyui datagrid设置行样式 不可删除某行
rowStyler: function (index,row) { if (parseInt(row.ksrs) > 0) { return 'color:red'; } }, onLoadSu ...
- IntelliJ IDEA安装配置
1. 从官网安装最新版IntelliJ Idea软件. 2. 激活使用 http://www.3322.cc/soft/37661.html 3. 配置eclipse快捷键 File-->Set ...
- 排序算法的C语言实现(上 比较类排序:插入排序、快速排序与归并排序)
总述:排序是指将元素集合按规定的顺序排列.通常有两种排序方法:升序排列和降序排列.例如,如整数集{6,8,9,5}进行升序排列,结果为{5,6,8,9},对其进行降序排列结果为{9,8,6,5}.虽然 ...
- ABP文档笔记 - 事件BUS
文档: ABP框架 - 领域事件(EventBus) EventBus & Domain Events ABP源码分析二十五:EventBus EventBus(事件总线) EventBus是 ...
- Answers to "Why are my jobs not running?"
from :https://community.oracle.com/thread/648581 This is one of the most common Scheduler questions ...
- markdowm写博客测试
markdowm测试文档 #include <bits/stdc++.h> using namespace std; int main() { printf("Hello Wor ...
- ZooKeeper之(六)应用实例
6.1 Java API 客户端要连接 Zookeeper服务器可以通过创建 org.apache.zookeeper.ZooKeeper 的一个实例对象,然后调用这个类提供的接口来和服务器交互. Z ...
- 【Java集合系列】---ArrayList
开篇前言--ArrayList中的基本方法 前面的博文中,小编主要简单介绍java集合的总体架构,在接下来的博文中,小编将详细介绍里面的各个类,通过demo.对比,来对java集合类进行更加深入的理解 ...
- Ubuntu批量修改文件名后缀
比如把当前文件夹下所有scss文件后缀改为less rename 's/\.scss/\.less/' ./*