您的位置:首页 > 理论基础 > 计算机网络

UI中的网络请求

2015-09-10 08:53 477 查看
@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; }

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  网络 interface