下载器
// // ViewController.m // NSConnection-0805 // // Created by apple on 14-8-5. // Copyright (c) 2014年 apple. All rights reserved. // #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> { NSMutableData *_totalData; long long _totalDataLenth; NSFileHandle *_fileHandle; NSURL *_url; NSURLConnection *_connection; } @property(nonatomic,strong)UIProgressView *progressView; @property(nonatomic,strong)UITextField *urlTextField; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _totalData=[[NSMutableData alloc]init]; self.view.backgroundColor=[UIColor greenColor]; //进度条的创建 _progressView=[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault]; _progressView.frame=CGRectMake(70, 150, 150, 20); _progressView.progressTintColor=[UIColor blueColor]; [self.view addSubview:_progressView]; //URL输入栏 _urlTextField=[[UITextField alloc]initWithFrame:CGRectMake(40, 200, 200, 30)]; _urlTextField.backgroundColor=[UIColor whiteColor]; [self.view addSubview:_urlTextField]; //创建下载按钮 UIButton *downLoad=[UIButton buttonWithType:UIButtonTypeCustom]; downLoad.frame=CGRectMake(100, 300, 120, 40); [downLoad setTitle:@"开始下载" forState:UIControlStateNormal]; [downLoad addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:downLoad]; NSFileManager *fileManager=[NSFileManager defaultManager]; [fileManager createFileAtPath:@"/Users/apple/Desktop/test1.mp4" contents:nil attributes:nil]; _fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/Users/apple/Desktop/test1.mp4"] ; // _url=[NSURL URLWithString:@"http://class.room/hdmv.mp4"]; // NSURLRequest *request=[NSURLRequest requestWithURL:_url]; // NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; // [connection start]; // _connection=[[NSURLConnection alloc]initWithRequest:request delegate:self]; } //按钮方法 - (void)down { [_fileHandle truncateFileAtOffset:0]; _url=[NSURL URLWithString:_urlTextField.text]; NSURLRequest *request=[NSURLRequest requestWithURL:_url]; // NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; // [connection start]; _connection=[[NSURLConnection alloc]initWithRequest:request delegate:self]; //开始下载 [_connection start]; // [_connection cancel]; } //连接失败响应方法 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"connection fail %@",error); } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"Response%@",response); _totalDataLenth=[response expectedContentLength]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"接收数据"); // [_totalData appendData:data]; [_fileHandle writeData:data]; // NSLog(@"==%d",[_totalData length]); NSLog(@"data %f",(float)[_fileHandle offsetInFile]/_totalDataLenth); _progressView.progress=(float)[_fileHandle offsetInFile]/_totalDataLenth; } //连接完成 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"connection finished"); [_fileHandle closeFile]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end