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 ...
随机推荐
- 树莓派3B(2)- 配置多个wifi,自动寻找可用网络
一.背景 在上篇<Raspberry Pi 3B 安装系统并联网>中,树莓派使用wifi连接,但是把树莓派带到公司,树莓派就连不了公司的wifi,要是支持连接多个wifi就好了,在此整理分 ...
- json转化为对象数组
1.ascx传值给aspx aspx页面 <%@ Page Title="" Language="C#" MasterPageFile="~/_ ...
- 聪明的搜索算法’ A*算法
A*算法 是一种启发式的搜索算法. 了解BFS.DFS或者Dijkstra算法的人应该知道.这些算法都是一种向四周盲目式搜索的方法. 启发式搜索: 启发式搜索就是在状态空间中的搜索 ...
- flask+apscheduler+redis实现定时任务持久化
在我们开发flask的时候,我们会结合apscheduler实现定时任务,我们部署到服务器上,会不会遇到这样的问题,每次我们部署后,我们重启服务后,原来的定时任务都需要重启,这样对我们经常迭代的项目肯 ...
- mysql 索引列为Null的走不走索引及null在统计时的问题
要尽可能地把字段定义为 NOT NULL,即使应用程序无须保存 NULL(没有值),也有许多表包含了可空列(Nullable Column)这仅仅是因为它为默认选项.除非真的要保存 NULL,否则就把 ...
- 转:Kafka 客户端TimeoutException问题之坑
原文出自:http://www.jianshu.com/p/2db7abddb9e6 各种TimeoutException问题 会抛出org.apache.kafka.common.errors.Ti ...
- oracle拆分一个连续的字符串
create or replace procedure pc( sss out varchar2)isstr varchar2(20):='ph,p,cod,do,cu';en integer:=i ...
- [转载]致创业者:APP已死 服务永生
前几日,有位创业者和我讲他在带领团队做一个将爱踢球的人集中在一起的App,我告诉他你的创业方向错了.原因在于你的目的是要为爱踢球的人提供服务,而你现在却在竭尽全力的做App,你应该做的是设计你为爱踢球 ...
- Docker部署Zabbix+Grafana监控
Docker部署Zabbix+Grafana监控 环境 centos 7 ; Docker 17.12.0-ce ; docker-compose version 1.20.1 2018-4-1 当前 ...
- springMVC源码分析--@ModelAttribute使用及运行原理
这一篇博客我们简单的介绍一下ModelAttribute的使用和运行原理. 1.首先@ModelAttribute是使用在方法或者上的,当使用在方法上时其作用于本身所在的Controller,在访问C ...