iOS小技能:版本升级原则(任何进步都比没有进步好) & 检查新版本方案(比较版本号大小的方法)

1,682 阅读5分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情

引言

  • 版本升级原则:任何进步都比没有进步好

在谷歌内部,大部分产品的改进都是渐进的,即使是新产品,也难以第一次就很完美。很多时候,快到新版本上线的截止日期,总有个别项目不能如期交付相应的功能,缺了一些功能的新版本确实让人不舒服。这个时候怎么办呢?是否再等一两天? 谷歌的做法是不等,因为可能永远没有完美的时候。将一个比原来更好一点儿的版本按时提供给用户,总比为了追求一个完美的版本,最后什么都提供不了好得多。

  • 根据appid检测是否有新版本

原文:kunnan.blog.csdn.net/article/det…

I 版本升级原则:任何进步都比没有进步好

1.1 谷歌的做法

在谷歌内部,大部分产品的改进都是渐进的,即使是新产品,也难以第一次就很完美。很多时候,快到新版本上线的截止日期,总有个别项目不能如期交付相应的功能,缺了一些功能的新版本确实让人不舒服。这个时候怎么办呢?是否再等一两天? 谷歌的做法是不等,因为可能永远没有完美的时候。将一个比原来更好一点儿的版本按时提供给用户,总比为了追求一个完美的版本,最后什么都提供不了好得多。

1.2 美国在2016年底有过一次通过限枪法案的机会

美国国会两党都提出了禁枪或者限枪法案,当然内容有所差异。简单来说,民主党提出的法案支持严格禁枪,而共和党的法案希望有条件限枪,即在卖枪之前先做比较详细的背景调查,确保拥枪人员无犯罪记录。但是,在随后不久的表决中,两个法案都没有获得通过,于是限枪就胎死腹中。

2016年底,两党的方案其实有很多共同之处,甚至可以说,共和党的方案是民主党的方案的子集,至少双方都同意有不良记录的人不能拥有枪支。如果能达成这样一个折中协议,总比没有结果好。但是双方都希望自己的诉求全部得到满足,最后的结果却是什么诉求都满足不了。

最好是更好的敌人

因为想不出让所有人都满意的方案而难以推进工作。很多时候,一个完美的结果需要完成很多改进,而不会一步到位。

世界上很多事情,其实本身很难一步到位。很多时候,一些人无所作为不是因为不想做事,而是一根筋地追求最好,最后什么也得不到。 (比如你一直想买大四房的房子,但最后往往还是连小房子也没有)

II 根据appid检测是否有新版本

按照位数进行依次比对,而不是去掉.分隔符再比较大小

比如3.1.0 和3.0.16 如果是先去掉.分隔符再比较大小,这样会导致误判,因为3016 比3.1.0更大。 除非提前规定约定好,版本号至少固定4位,或者五位。 即 3.1.00 和3.0.16 . 但是这样容易因为人的疏忽而导致判断失误

2.1 去掉.分隔符再比较大小

  • 根据appid检测是否有新版本
    
#pragma mark 检查版本 STOREAPPID
- (void)checkTheVersionWithappid:(NSString*)appid{


    
    [QCTNetworkHelper getWithUrl:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",appid] params:nil successBlock:^(NSDictionary *result) {
        if ([[result objectForKey:@"results"] isKindOfClass:[NSArray class]]) {
            NSArray *tempArr = [result objectForKey:@"results"];
            if (tempArr.count) {
                
                
                NSString *versionStr =[[tempArr objectAtIndex:0] valueForKey:@"version"];
                NSString *appStoreVersion = [versionStr stringByReplacingOccurrencesOfString:@"." withString:@""] ;
                
                
                if (appStoreVersion.length==2) {
                    appStoreVersion  = [appStoreVersion stringByAppendingString:@"0"];
                }else if (appStoreVersion.length==1){
                    appStoreVersion  = [appStoreVersion stringByAppendingString:@"00"];
                }

                NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
                NSString* currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@""];
                
                currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
                // 去除点
                
                if (currentVersion.length==2) {
                    currentVersion  = [currentVersion stringByAppendingString:@"0"];
                }else if (currentVersion.length==1){
                    currentVersion  = [currentVersion stringByAppendingString:@"00"];
                }

//                "Discover_a_new_version"= "发现新版本";
//                "Whethertoupdate" = "是否更新";
//                "Illtalkaboutitlater"= "稍后再说";
//                "Update now" = "立即去更新";
//                "Unupdate"= "取消更新";

                NSLog(@"currentVersion: %@",currentVersion);
//                2020-03-07 20:11:55.867708+0800 Housekeeper[2777:849229] currentVersion: 310
//                2020-03-07 20:11:55.868262+0800 Housekeeper[2777:849229] appStoreVersion: 3016

                NSLog(@"appStoreVersion: %@",appStoreVersion);

                if([self compareVesionWithServerVersion:versionStr]){// 采用新的判断方式: 按照位数进行依次比对进行版本号大小判断
                    
//                
                
//                if([appStoreVersion floatValue] > [currentVersion floatValue]){// 废弃旧的判断方式:去掉.分隔符再比较大小
                    
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@%@",@"发现新版本",versionStr] message:@"是否更新?" preferredStyle:UIAlertControllerStyleAlert];
                    //                "Illtalkaboutitlater"= "稍后再说";
                    //                "Update now" = "立即去更新";
                    //                "Unupdate"= "取消更新";

                    [alertController addAction:[UIAlertAction actionWithTitle:@"稍后再说" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                        NSLog(@"取消更新");
                    }]];
                    [alertController addAction:[UIAlertAction actionWithTitle:@"立即去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appid]];
                        if (@available(iOS 10.0, *)) {
                            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                            }];
                        } else {
                            // Fallback on earlier vesions
                            [[UIApplication sharedApplication] openURL:url];
                        }
                    }]];
                    [[QCT_Common getCurrentVC] presentViewController:alertController animated:YES completion:nil];
                }
            }
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"检查版本错误: %@",error);
    }];
}


2.2 按照位数进行依次比对

-(BOOL)compareVesionWithServerVersion:(NSString *)version{
    NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];

//    NSString* currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@""];

    NSString* currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"];

    NSArray *versionArray = [version componentsSeparatedByString:@"."];//服务器返回版
    NSArray *currentVesionArray = [currentVersion componentsSeparatedByString:@"."];//当前版本
    NSInteger a = (versionArray.count> currentVesionArray.count)?currentVesionArray.count : versionArray.count;
    
    for (int i = 0; i< a; i++) {
        NSInteger a = [[versionArray safeObjectAtIndex:i] integerValue];
        NSInteger b = [[currentVesionArray safeObjectAtIndex:i] integerValue];
        if (a > b) {
            NSLog(@"有新版本");
            return YES;
        }else if(a < b){
            return NO;
    }
    }
    return NO;
}