应用内部提示更新

当打开应用时主动提示用户更新最新版本,如果确定更新就跳转到appStore下载页
提示内容:最新版本的名字、更新内容

- (void)upgradeVersionInfo{
  NSError *error;
  //kAPP_URL : http://itunes.apple.com/lookup?id=
  //kAppId : 在iTunes connect上申请的APP ID  1062459942

  NSString *appID;
  appID = @"1062459942";
  NSString *appUrl = @"http://itunes.apple.com/lookup?id=";

  NSString *urlStr = [NSString stringWithFormat:@"%@%@", appUrl, appID];
  NSURL *url = [NSURL URLWithString:urlStr];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

  if (error) {
      NSLog(@"%@", error.description);
      return;
  }
  NSArray *resultArray = [appInfoDict objectForKey:@"results"];
  if (![resultArray count]) {
      NSLog(@"error : resultArray == nil");
      return;
  }
  HMLog(@"resultArray -= %@",resultArray);

  //数组里就一个字典
  NSDictionary *infoDict = [resultArray objectAtIndex:0];
  //获取服务器上应用的最新版本号
  NSString *updateVersion = infoDict[@"version"];
  //获取服务器上应用的最新名字
  NSString *trackName = infoDict[@"trackName"];
  //获取服务器上应用最新的版本新功能
  NSString *note = [infoDict objectForKey:@"releaseNotes"];
  //应用下载页
  self.trackViewUrl = infoDict[@"trackViewUrl"];
  //获取当前设备中应用的版本号
  NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"] ;
  //判断两个版本是否相同
  if ([updateVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending) {
      NSString *titleStr = [NSString stringWithFormat:@"更新提示:%@", trackName];
      UIAlertController *alertController = [UIAlertController alertControllerWithTitle:titleStr message:note preferredStyle:UIAlertControllerStyleAlert];
    
      UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"稍后再说" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
      UIAlertAction *upgradeAction = [UIAlertAction actionWithTitle:@"现在升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
      }];
      [alertController addAction:cancelAction];
      [alertController addAction:upgradeAction];
      [self presentViewController:alertController animated:YES completion:nil];
  }
}

你可能感兴趣的:(应用内部提示更新)