检测版本号更

作者: 傲骨天成科技 | 来源:发表于2016-11-22 11:11 被阅读111次

    1.一定要先配置自己项目在商店的APPID(从苹果开发者中心登录自己的账号可看),配置完最好在真机上运行才能看到完全效果哦!

    define STOREAPPID @"1104867082" // 宏定义APPID

    /**

    • 天朝专用检测app更新
      一句代码实现检测更新,很简单哦 (需要在viewDidAppear完成时,再调用改方法。不然 在网速飞快的时候,会出现一个bug,就是当前控制器viewDidLoad调用的话,可能当前视 图还没加载完毕就需要推出UIAlertAction)
      */
      -(void)hsUpdateApp
      {
      //2先获取当前工程项目版本号
      NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
      NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];

      //3从网络获取appStore版本号
      NSError *error;
      NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]] returningResponse:nil error:nil];
      if (response == nil) {
      NSLog(@"你没有连接网络哦");
      return;
      }
      NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
      if (error) {
      NSLog(@"hsUpdateAppError:%@",error);
      return;
      }
      // NSLog(@"%@",appInfoDic);
      NSArray *array = appInfoDic[@"results"];
      NSDictionary *dic = array[0];
      NSString *appStoreVersion = dic[@"version"];
      //打印版本号
      NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);
      //设置版本号
      currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
      if (currentVersion.length==2) {
      currentVersion = [currentVersion stringByAppendingString:@"0"];
      }else if (currentVersion.length==1){
      currentVersion = [currentVersion stringByAppendingString:@"00"];
      }
      appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
      if (appStoreVersion.length==2) {
      appStoreVersion = [appStoreVersion stringByAppendingString:@"0"];
      }else if (appStoreVersion.length==1){
      appStoreVersion = [appStoreVersion stringByAppendingString:@"00"];
      }

      //4当前版本号小于商店版本号,就更新
      if([currentVersion floatValue] < [appStoreVersion floatValue])
      {
      UIAlertController *alercConteoller = [UIAlertController alertControllerWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]] preferredStyle:UIAlertControllerStyleAlert];
      UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      //此处加入应用在app store的地址,方便用户去更新,一种实现方式如下
      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", STOREAPPID]];
      [[UIApplication sharedApplication] openURL:url];
      }];
      UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

      }];
      [alercConteoller addAction:actionYes];
      [alercConteoller addAction:actionNo];
      [self presentViewController:alercConteoller animated:YES completion:nil];
      

      }else{
      NSLog(@"版本号好像比商店大噢!检测到不需要更新");
      }

    }

    相关文章

      网友评论

        本文标题: 检测版本号更

        本文链接:https://www.haomeiwen.com/subject/uauppttx.html