美文网首页
iOS项目版本判断

iOS项目版本判断

作者: 正直走 | 来源:发表于2017-01-21 23:33 被阅读82次

    我们在开发中经常需要判断当前版本去做相应的操作,比如跳转到AppStore进行更新或判断该App是否第一次打开等。首先我们应该知道xcode中的显示位置如下:

    image.png
    Image2.png
    1、首先我们可以在General的Version或infoPlist字典中的Bundle versions string, short键看到,二者是一样的,即我们在Version中更改版本之后后者也会自动更改。
    我们可以通过代码打印系统的infoPlist中的字典
    NSString *infoDic = [NSBundle mainBundle].infoDictionary;

    如下是该字典中的部分键值

    {
        BuildMachineOSBuild = 15G1004;
        CFBundleDevelopmentRegion = en;
        CFBundleExecutable = "\U90a3\U4f60\U8bf4\U5427";
        CFBundleIdentifier = "com.example";
        CFBundleInfoDictionaryVersion = "6.0";
        CFBundleName = "\U90a3\U4f60\U8bf4\U5427";
        CFBundleNumericVersion = 16809984;
        CFBundlePackageType = APPL;
        CFBundleShortVersionString = "1.0";//项目版本
    }
    

    所以我们通过字典中的CFBundleShortVersionString键得到当前版

     NSString *currentVersion = [infoDic objectForKey:@"versionKey"];```
       
    2、判断当前版本是否是第一次打开
        + (void)judgeNewVersion
        {
          NSString *versionKey = @"CFBundleShortVersionString";
          //infoPlist中的版本
          NSString * version = [NSBundle mainBundle].infoDictionary[versionKey];
          //取出上次存储的版本号
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
          NSString *str = [defaults objectForKey:@"CFBundleShortVersionString"];
        
          //对比两次版本后是否相同
          UIWindow*window = [UIApplication sharedApplication].keyWindow;
          if (![version isEqualToString:str]) { //如果当前版本第一次打开,做相应的操作(比如显示新特性控制器控制器)
            window.rootViewController =  [[新特性控制器 alloc] init];
            //将新版本储存到沙盒中
            [defaults setObject:version forKey:@"CFBundleShortVersionString"];
            [defaults synchronize];
          } else {//如果当前版本不是第一次打开直接显示默认根控制器
            [UIApplication sharedApplication].statusBarHidden = NO;
            window.rootViewController =  [[根控制器 alloc]init];
          }
        }
       <br>
       3、判断当前版本和服务器版本是否一样
        + (void)judgeNewVersion
        {
            NSString *versionKey = @"CFBundleShortVersionString";
            //infoPlist中的版本
            NSString *version = [NSBundle mainBundle].infoDictionary[versionKey];
            //服务器请求数据获得最新的版本
            NSString *newVersion = getThelatestVersionFromServer;
        
            //对比两次版本后是否相同
            UIWindow*window = [UIApplication sharedApplication].keyWindow;
            if(![version isEqualToString:newVersion]){//当不是最新版本的时候提示用户更新
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"更新版本有更好体验,请下载最新版本" delegate:self cancelButtonTitle:@"立即更新" otherButtonTitles:@"暂不更新",nil];
                [alert show];
            }else{//如果已经是最新版本直接显示默认根控制器
                [UIApplication sharedApplication].statusBarHidden = NO;
                window.rootViewController =  [[HMTabBarController alloc]init];
            }
        }

    相关文章

      网友评论

          本文标题:iOS项目版本判断

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