如何简单的下载一些小文件并保存在一个路径中(有些方法进行了封装)

来源:互联网 发布:js获取div id值 编辑:程序博客网 时间:2024/05/22 17:27
#import "HomeViewController.h"@interface HomeViewController () <NSURLConnectionDataDelegate>{    UITextField *field;        NSFileHandle *handleFile;        UIProgressView *progressView;        long long totalLength;        NSURLConnection *connect;}@end@implementation HomeViewController- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void) creatProgress{    progressView = [[UIProgressView alloc] init];    progressView.frame = CGRectMake(20, 300, 250, 20);    progressView.progress = 0;    [self.view addSubview:progressView];}//封装progress//- (UIProgressView *) progressView : (CGRect)fram andValue :(int)ValueProgress//{//    progressView = [[UIProgressView alloc] init];//    progressView.frame = fram;//    progressView.progress = ValueProgress;//    [self.view addSubview:progressView];//    return progressView;//}- (void) creatTextField{    field = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 280, 30)];    field.backgroundColor = [UIColor blackColor];    field.textColor = [UIColor redColor];    [self.view addSubview:field];}//封装textField//- (UITextField *) field :(CGRect)fram andBackgroundColor :(UIColor *)backColor andTextColor: (UIColor *)textColor//{//    field = [[UITextField alloc] initWithFrame:fram];//    field.backgroundColor = backColor;//    field.textColor = textColor;//    [self.view addSubview:field];//    return field//}- (void) creatDownBtn{    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.backgroundColor = [UIColor yellowColor];    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];    [btn setTitle:@"下载" forState:UIControlStateNormal];    btn.frame = CGRectMake(30, 200, 100, 30);    [btn addTarget:self action:@selector(downLoad) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];    }//- (UIButton *) creatBtn :(UIColor *)backgroundColor andName :(NSString *)name andFram :(CGRect)fram andAction :(SEL)action//{//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];//    btn.backgroundColor = backgroundColor;//    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//    [btn setTitle:name forState:UIControlStateNormal];//    btn.frame = fram;//    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];//    [self.view addSubview:btn];//    return btn;//}- (void)viewDidLoad{    [super viewDidLoad];    [self creatTextField];    [self creatDownBtn];    [self creatProgress];    }- (void) downLoad{    NSFileManager *fileManager = [NSFileManager defaultManager];    NSURL *url = [NSURL URLWithString:field.text];    NSString *path0 = @"/Users/gaozhenyusky/Desktop/下载测试/test";    NSString *path = [path0 stringByAppendingPathExtension:[url pathExtension]];    [fileManager createFileAtPath:path contents:nil attributes:nil];    handleFile = [NSFileHandle fileHandleForWritingAtPath:path];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    connect = [NSURLConnection connectionWithRequest:request delegate:self];    [connect start];}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSLog(@"%@",response);    totalLength = response.expectedContentLength;}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [handleFile writeData:data];    float progressValue = (float)[handleFile offsetInFile]/totalLength;    progressView.progress = progressValue;    NSLog(@"%f",progressValue);}- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"出错了!!!");    }- (void) connectionDidFinishLoading:(NSURLConnection *)connection{    [handleFile closeFile];}

0 0
原创粉丝点击