// // ViewController.m // UI4_斷點下載 // // Created by qianfeng on 15/8/19. // Copyright (c) 2015年 ZBC. All rights reserved. // #import "ViewController.h" @interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate> { NSURL *_url; NSURLConnection *_connection; //文件讀寫 NSFileHandle *_fileHandle; //下載文件保存路徑 NSString *_filePath; //文件真實大小 long long _expectSize; //已經下載的大小 long long _readSize; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _url=[NSURL URLWithString:[@"http://192.168.84.188/新版上線流程整理.pdf" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//解碼 NSLog(@"%@",NSHomeDirectory()); NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; _expectSize=[[defaults objectForKey:@"xpectsize"] longLongValue]; if (_expectSize) { _filePath=[NSHomeDirectory() stringByAppendingString:@"/Library/Caches/新版上線流程整理.pdf"]; _fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:_filePath]; _readSize=[_fileHandle readDataToEndOfFile].length; CGFloat ratio=1.0*_readSize/_expectSize; _progressLabel.text=[NSString stringWithFormat:@"%.2f%%",ratio*100]; _progressView.progress=ratio; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)startBtn:(UIButton *)sender { if ([sender.titleLabel.text isEqualToString:@"開始下載"]) { [sender setTitle:@"暫停" forState:UIControlStateNormal]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:_url]; NSString *range=[NSString stringWithFormat:@"bytes=%lld-",_readSize]; [request setValue:range forHTTPHeaderField:@"Range"]; _connection=[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:_url] delegate:self]; } else { [sender setTitle:@"開始下載" forState:UIControlStateNormal]; [_connection cancel]; } } #pragma mark---下載協議 //下載錯誤 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",error.localizedDescription); } //接收到數據 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { //獲取到 下載的大小 _readSize+=data.length; //一直追加 [_fileHandle seekToEndOfFile]; [_fileHandle writeData:data]; CGFloat ratio=1.0*_readSize/_expectSize; _progressLabel.text=[NSString stringWithFormat:@"%.2f%%",ratio*100]; _progressView.progress=ratio; } //接收到響應 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"%lld",_expectSize); if (!_expectSize) { _filePath=[NSHomeDirectory() stringByAppendingString:@"/Library/Caches/新版上線流程整理.pdf"]; NSLog(@"%@",_filePath); _expectSize=response.expectedContentLength; NSFileManager *manager=[NSFileManager defaultManager]; if (![manager fileExistsAtPath:_filePath]) { [manager createFileAtPath:_filePath contents:nil attributes:nil]; } _fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:_filePath]; NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; [defaults setObject:@(_expectSize) forKey:@"xpectsize"]; [defaults synchronize]; } } //下載完成 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"下載完成"); [_fileHandle closeFile]; } @end