iOS实现断点续传
来源:互联网 发布:网吧电脑优化 编辑:程序博客网 时间:2024/05/17 18:01
NSURLConnection 实现断点续传
controller
//写文件的文件句柄
@property (nonatomic,strong) NSFileHandle *writeHandle;
//当前下载总的字节数
@property (nonatomic) long long currentDownloadByte;
//总文件大小
@property (nonatomic) CGFloat fileSize;
@end
@implementation ViewController
{
NSURLConnection *conn;
}
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@”%@”,[self getFilePath]);//本地文件大小
NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:[self getFilePath] error:nil];self.currentDownloadByte = fileInfo.fileSize;
self.fileSize = [[[NSUserDefaults standardUserDefaults] objectForKey:@”fileSize”] floatValue];if(self.fileSize != 0)
{
self.proView.progress = self.currentDownloadByte / self.fileSize;
self.proLabel.text = [NSString stringWithFormat:@”%.2f%%”,self.proView.progress * 100];
}
}(IBAction)pause:(id)sender {
//暂停下载
[conn cancel];//文件关闭
[self.writeHandle closeFile];
}
//文件操作 NSFileHandle —-> 沙盒
-(NSString *)getFilePath
{
return [NSString stringWithFormat:@”%@/Documents/qq.dmg”,NSHomeDirectory()];
}
(IBAction)start:(id)sender {
NSURL *url = [NSURL URLWithString:KCYQQURL];NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//Range
//告诉服务器 下载数据范围
[req setValue:[NSString stringWithFormat:@”bytes=%lld-“,self.currentDownloadByte] forHTTPHeaderField:@”Range”];conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
}
-(void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse )response
{
//得到文件大小
self.fileSize = response.expectedContentLength + self.currentDownloadByte;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:self.fileSize] forKey:@"fileSize"];[[NSUserDefaults standardUserDefaults] synchronize];//创建文件if(![[NSFileManager defaultManager] fileExistsAtPath:[self getFilePath]]){ [[NSFileManager defaultManager] createFileAtPath:[self getFilePath] contents:nil attributes:nil];}//打开文件 --> 文件指针 默认指向文件头部self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:[self getFilePath]];//把文件指针指向文件的尾部[self.writeHandle seekToEndOfFile];
}
-(void)connection:(NSURLConnection )connection didReceiveData:(NSData )data
{
//把数据写入到文件
[self.writeHandle writeData:data];
self.currentDownloadByte += data.length;self.proView.progress = self.currentDownloadByte / self.fileSize;self.proLabel.text = [NSString stringWithFormat:@"%.2f%%",self.proView.progress * 100];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//关闭文件
[self.writeHandle closeFile];
}
NSURLSeesion 实现断点续传
@property (nonatomic,strong) NSURLSession *session;
@property (nonatomic,strong) NSURLSessionDownloadTask *downloadTask;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
-(void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
self.proView.progress = totalBytesWritten / (CGFloat)totalBytesExpectedToWrite;
self.proLabel.text = [NSString stringWithFormat:@”%.2f%%”,self.proView.progress * 100];
}
//下载完成的方法 —> 处理下载完成的文件
//location 当前下载数据的缓存文件路径
-(void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask didFinishDownloadingToURL:(NSURL *)location
{
//把文件复制到自己的文件夹里
NSString *qqPath = [NSString stringWithFormat:@"%@/Documents/qq.dmg",NSHomeDirectory()];if(![[NSFileManager defaultManager] fileExistsAtPath:qqPath]){ [[NSFileManager defaultManager] copyItemAtURL:location toURL:[NSURL fileURLWithPath:qqPath] error:nil];}
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}(IBAction)pause:(id)sender {
//暂停下载
//resumeData 暂停之前 所有下载的数据
[self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
[[NSUserDefaults standardUserDefaults] setObject:resumeData forKey:@”resumeData”];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
}(IBAction)start:(id)sender {
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@”http://dldir1.qq.com/qqfile/QQforMac/QQ_V4.0.1.dmg“]];NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@”resumeData”];
if(data.length == 0)
{
self.downloadTask = [self.session downloadTaskWithRequest:req];
}
else
{
self.downloadTask = [self.session downloadTaskWithResumeData:data];
}[self.downloadTask resume];
}
- ios实现断点续传
- ios断点续传实现
- iOS- 断点续传实现原理
- iOS实现断点续传
- IOS NSURLSessionDataTask实现断点续传
- iOS实现断点续传
- ios 实现断点续传 一 nsurlconnection
- ios 实现断点续传 一 nsurlconnection
- iOS - NSURLSession实现断点续传下载
- ios 实现简单的断点续传下载 nsurlconnection
- iOS开发 - NSURLConnection实现断点续传下载
- iOS开发 - NSURLSession实现断点续传下载
- ios断点续传:NSURLSession和NSURLSessionDataTask实现
- IOS断点续传:NSURLSession和NSURLSessionDataTask实现
- iOS断点续传:NSURLSession和NSURLSessionDataTask实现
- iOS开发之断点续传原理与实现
- iOS开发 使用NSURLConnection实现断点续传下载
- IOS 断点续传
- 161107
- jeesite框架CMS中英文站点切换解决方案
- 带头结点的非循环单向链表
- 记录自己的收藏夹
- 16-8-30问题
- iOS实现断点续传
- SuperMap iServer 8C(2017),一大波新特性来袭
- Socket编程基础
- React Native开发环境搭建
- 一个iOS模块化开发解决方案
- 第十周项目二(2)输出10000以内回文数
- schema
- 文件及目录管理
- IOS开发基础06(界面通信(界面传值):属性传值、代理传值、闭包传值)