如何簡單的下載一些小文件並保存在一個路徑中(有些方法進行了封裝)

#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];
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章