码迷,mamicode.com
首页 > 其他好文 > 详细

UI中的网络请求

时间:2015-09-10 11:16:07      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:interface   网络   

@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;

    

}



UI中的网络请求

标签:interface   网络   

原文地址:http://10594302.blog.51cto.com/10584302/1693313

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!