@interface ViewController ()<NSURLConnectionDataDelegate>

{

    CGFloat totleLength;

    NSMutableData *filedata;

    BOOL isDownload;

    

    CGFloat reciveTotle;

    NSString *filePath;

    NSURLConnection *_connection;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    totleLength = [[userDefaults objectForKey:@"totleLength"] floatValue];

    reciveTotle = [[userDefaults objectForKey:@"reciveTotle"] floatValue];

    if (reciveTotle > 0) {

        CGFloat progress = reciveTotle / totleLength;

        self.progressView.progress = progress;

        self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",progress * 100];

    }

}

- (IBAction)btnClick:(UIButton *)sender {

    if (isDownload) {

        return;

    }

    NSURL *url = [NSURL URLWithString:@"http://free2.macx.cn:8182/game/BombSquadX401.dmg"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    if (reciveTotle > 0) {

        NSString *value = [NSString stringWithFormat:@"bytes=%d-",(int)reciveTotle];

        [request setValue:value forHTTPHeaderField:@"Range"];

    }

    

    

   _connection = [NSURLConnection connectionWithRequest:request delegate:self];

    isDownload = true;

    

    NSString *str = url.absoluteString;

    NSString *strName = [str lastPathComponent];

    filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",strName];

    

    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

        

        [[NSFileManager defaultManager]createFileAtPath:filePath contents:nil attributes:nil];

    }


}

- (IBAction)pauseAction:(UIButton *)sender {

    

    [_connection cancel];

    _connection  = nil;

    

    [self appendFileData:filedata];

    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults setObject:@(reciveTotle) forKey:@"reciveTotle"];

    [userDefaults setObject:@(totleLength) forKey:@"totleLength"];

    

    [userDefaults synchronize];

    

    isDownload = NO;

    

}

#pragma mark-NSURLConnectionDataDelegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{

    

    filedata = [[NSMutableData alloc]init];

    NSDictionary *dic = response.allHeaderFields;

    NSNumber *number = [dic objectForKey:@"Content-Length"];

    totleLength = [number floatValue];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    

    [filedata appendData:data];

    reciveTotle += data.length;

    self.progressView.progress = reciveTotle / totleLength;

    self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",self.progressView.progress * 100];

    if (filedata.length >= 500 * 1000) {

        [self appendFileData:filedata];

        filedata.data = nil;

    }

}


- (void)appendFileData:(NSData *)data

{

    if (data.length == 0) {

        return;

    }

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

    [fileHandle seekToEndOfFile];

    [fileHandle writeData:data];

    

    [fileHandle closeFile];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    

    if(filedata.length < 500 * 1000){

        [self appendFileData:filedata];

        [filedata setData:nil];

        

         [filedata writeToFile:filePath atomically:YES];

    }

    self.progressLabel.text = @"下载完成";

    isDownload = false;

    

}