UI中的网络请求-创新互联

@interface ViewController ()

安乡网站建设公司成都创新互联公司,安乡网站设计制作,有大型网站制作公司丰富经验。已为安乡上1000家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的安乡做网站的公司定做!

{

  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;

 

}


创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前题目:UI中的网络请求-创新互联
地址分享:http://scjbc.cn/article/dssiod.html

其他资讯