一个下载工具

一个下载工具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



  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值