- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",NSHomeDirectory());
//取得已下载数据大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
receiveTotal = [[userDefaults objectForKey:@"ReceiveTotal"] doubleValue];
total = [[userDefaults objectForKey:@"Total"] doubleValue];
//显示上一次下载的进度
if (total > 0) {
CGFloat progress = receiveTotal/total;
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress * 100];
}
}
- (IBAction)downLoadClick:(UIButton *)sender {
if (!isDownLoad) {
//1.构建URL
NSURL *url = [NSURL URLWithString:@"http://s.qdcdn.com/lovebizhi/LoveWallpaper4Mac.dmg"];
//2.构建Request
NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
//断点续传
if (receiveTotal > 0) {
//设置续传位置
NSString *position = [NSString stringWithFormat:@"bytes=%d-",(int)receiveTotal];
[mRequest setValue:position forHTTPHeaderField:@"Range"];
}
//3.发送异步请求
connection = [NSURLConnection connectionWithRequest:mRequest delegate:self];
isDownLoad = YES;
//获取字符串
NSString *urlStr = url.absoluteString;
//获取urlStr的最后一部分
NSString *fileName = [urlStr lastPathComponent];
//写入文件
filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",fileName];
//创建文件
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}
}
//暂停下载
- (IBAction)pauseClick:(UIButton *)sender {
//取消下载连接
[connection cancel];
connection = nil;
//将缓冲数据写入文件
[self appendFileData:self.receiveData];
//在本地保存已下载文件的大小和文件总大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@(receiveTotal) forKey:@"ReceiveTotal"];
[userDefaults setObject:@(total) forKey:@"Total"];
//将数据同步写入文件
[userDefaults synchronize];
isDownLoad = NO;
}
#pragma mark-NSURLConnectionDataDelegate
//服务器响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{
self.receiveData = [[NSMutableData alloc]init];
//判断总大小是否为0,如果为0,说明从起始位置开始下,获取文件总大小
if (total == 0) {
//获取所有的响应头
NSDictionary *fields = response.allHeaderFields;
NSLog(@"fields is %@",fields);
//获取数据的总大小
NSNumber *length = [fields objectForKey:@"Content-Length"];
total = [length doubleValue];
NSLog(@"total is %.2f",total);
}
}
//接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receiveData appendData:data];
receiveTotal += data.length;
//计算进度
double progress = receiveTotal / total;
//刷新UI
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%",progress * 100];
//判断缓冲的数据是否大于500KB
if (self.receiveData.length >= 500 * 1024) {
//写入数据
[self appendFileData:self.receiveData];
//清空
self.receiveData.data = nil;
}
}
//数据传输完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//将最后一个缓冲文件写入
if (self.receiveData.length < 500 * 1024) {
//写入数据
[self appendFileData:self.receiveData];
//清空
self.receiveData.data = nil;
}
_progressLabel.text = @"下载完成";
self.progressView.progress = 0;
isDownLoad = NO;
}
- (void)appendFileData:(NSData *)data{
if (data.length == 0) {
return;
}
//使用NSFileHandle写文件,此文件必须已经创建,NSFileHandle不会创建文件
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
//将数据插入到写入点
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
//关闭文件,确保写入完成
[fileHandle closeFile];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
原文地址:http://10577688.blog.51cto.com/10567688/1693330