应用内部提示更新

当打开应用时主动提示用户更新最新版本,如果确定更新就跳转到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];
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容