记录历程第一篇!ios断点续传

五迷三道 提交于 2020-03-02 18:46:16
// #import "ViewController.h" #define QQURL @"http://211.162.77.51/data2/dmg/aabb46ab7cbb26fbad6f16cb1c7f5a58/QQ_V3.0.2.dmg" @interface ViewController () { NSURLConnection * _connection; NSString * _fileName; unsigned long long _totalSize; unsigned long long _downLoadSize; UIProgressView * _proView; NSFileHandle * _fileHandle; } @end @implementation ViewController - (void)dealloc { [_proView release]; [_connection release]; [_fileHandle closeFile]; [_fileHandle release]; [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; UIButton * b = [[UIButton alloc]initWithFrame:CGRectMake(0, 30, 100, 30)]; b.backgroundColor=[UIColor blueColor]; [b setTitle:@"Tap" forState:UIControlStateNormal]; [b addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:b]; _proView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 80, 320, 20)]; [self.view addSubview:_proView]; NSLog(@"path is %@",NSHomeDirectory()); } static bool start=YES; - (void)click:(UIButton *)b { if (start) { // 如果没有保存资源名就是第一次下载该资源 if (_fileName.length == 0) { NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:QQURL]]; _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; }else{ NSString * path = [NSString stringWithFormat:@"%@/%@",NSHomeDirectory(),_fileName]; NSFileManager * manager = [NSFileManager defaultManager]; // 文件的属性会读取在字典当中 NSDictionary * fileDic = [manager attributesOfItemAtPath:path error:nil]; // 读取大小 NSNumber * num = [fileDic objectForKey:NSFileSize]; _downLoadSize = num.unsignedLongLongValue; NSLog(@"已经下载了%llu个字节",_downLoadSize); NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:QQURL]]; // 写RANGE字段 RANGE:bytes=1000- [request addValue:[NSString stringWithFormat:@"bytes=%qu-",_downLoadSize] forHTTPHeaderField:@"RANGE"]; _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; } }else{ if (_connection) { [_connection cancel]; [_connection release]; _connection = nil; } } start = !start; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - NSURLConnectionDataDelegate - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if (_fileName.length == 0) { NSHTTPURLResponse * res = (NSHTTPURLResponse *)response; _totalSize = [res expectedContentLength]; _fileName = [[res suggestedFilename] retain]; NSFileManager * manager = [NSFileManager defaultManager]; NSString * path = [NSString stringWithFormat:@"%@/%@",NSHomeDirectory(),_fileName]; if (![manager fileExistsAtPath:path]) { [manager createFileAtPath:path contents:nil attributes:nil]; } _fileHandle = [[NSFileHandle fileHandleForUpdatingAtPath:path] retain];//加方法retain,防止变成野指针 } sum = _downLoadSize; } unsigned long long sum; - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_fileHandle writeData:data]; sum += data.length; CGFloat f = (CGFloat)sum/_totalSize; _proView.progress = f; NSLog(@"%f %llu %llu",f,sum,_totalSize); } - (void) connectionDidFinishLoading:(NSURLConnection *)connection { [_fileHandle closeFile]; [_connection cancel]; [_connection release]; } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [_fileHandle closeFile]; [_connection cancel]; [_connection release]; } @end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!