一个下载工具URL利用textFiled输入
//
// ViewController.m
// laji
//
// Created by apple on 14-8-5.
// Copyright (c) 2014年 dengshiru. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <NSURLConnectionDataDelegate>
{
NSMutableData *_totalData;
long long _totalLength;
NSFileHandle *_fileHandle;
UIProgressView *progressView;
UITextField *text1;
}
@end
@implementation ViewController
-(void)textFileds
{
text1= [[UITextField alloc] init];
text1.frame = CGRectMake(10, 100, 300, 20);
[text1 setBorderStyle:UITextBorderStyleRoundedRect];
// UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 300, 30)];
//[self.text1.text = @"dddd"];
NSLog(@"gan");
[self.view addSubview:text1];
NSLog(@"tamade");
}
-(void)Buttons
{
UIButton *go = [UIButton buttonWithType:UIButtonTypeCustom];
go.frame = CGRectMake(150, 200, 30, 20);
[go addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
[go setTitle:@"go" forState:UIControlStateNormal];
[go setTitleColor: [UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:go];
}
-(void)progress
{
//progressView = [[UIProgressView alloc]init];
progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
progressView.frame = CGRectMake(100, 200, 100, 20);
[self.view addSubview:progressView];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self Buttons];
[self textFileds];
[self progress];
//[self gogo];
}
-(void)gogo
{
_totalData = [NSMutableData data];
NSString *dict = NSHomeDirectory();
NSString *driction = [dict stringByAppendingString:@"/Documents/text.mp3"];
[@"" writeToFile:driction atomically:YES encoding:NSUTF8StringEncoding error:nil];
//创建文件
NSFileManager *fileManager = [NSFileManager defaultManager];
//打开文件
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:driction];
[fileManager createFileAtPath:driction contents:nil attributes:nil];
NSLog(@"%@",dict);
NSURL *url = [NSURL URLWithString:text1.text];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
-(void)go:(UIButton*)sender
{
[self gogo];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - NSURLConnectionDataDelegate
//4. 只有出错才会到这里
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Failed: %@", error);
}
//1. 先接受到响应信息,能够知道等下接受的数据是什么样的
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Response: %@", response);
NSLog(@"Total length: %lld", [response expectedContentLength]);
_totalLength = response.expectedContentLength;
}
//NSFileHandle
//2. 分次接受数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// [_totalData appendData:data];
[_fileHandle writeData:data];
float progress1 = (float)[_fileHandle offsetInFile]/_totalLength;
NSLog(@"Data: %.2f%%", progress1 * 100);
progressView.progress = progress1;
}
//3. 下载完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"finished...");
}
@end