ios -版本号判断

需求:

app升级,需要对比本地和线上的版本号

实现方法:

方法一、利用系统自带方法实现

    NSString *currentVersion = @"1.1.5";
    NSString *appStoreVersion = @"1.1.6";
    
    if ([appStoreVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending) {
     NSLog(@"有新版本1");
}

NSOrderedSame(同序)
NSOrderedDescending(降序)
NSOrderedAscending(升序)

方法二、规定最大位数,不足就添加零

- (BOOL)CompareLastVersion:(NSString *)appStoreVersion withCurrentVersion:(NSString *)currentVersion{
    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"];
    }
    
    //5当前版本号小于商店版本号,就更新
    if([currentVersion floatValue] < [appStoreVersion floatValue])
    {
//      block(currentVersion,dic[@"version"],[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", appid],YES);
        return YES;
    }else{
        //        NSLog(@"版本号好像比商店大噢!检测到不需要更新");
//      block(currentVersion,dic[@"version"],[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", appid],NO);
        
        return NO;
    }

}

方法三、通过“、”来生成数组,在对比每个数字,有大的则返回

- (BOOL)isEqualByCompareLastVersion:(NSString *)lastVersion withCurrentVersion:(NSString *)currentVersion {
    NSMutableArray *lastVersionArray = [[lastVersion componentsSeparatedByString:@"."] mutableCopy];
    NSMutableArray *currentVersionArray = [[currentVersion componentsSeparatedByString:@"."] mutableCopy];
    int modifyCount = abs((int)(lastVersionArray.count - currentVersionArray.count));
    if (lastVersionArray.count > currentVersionArray.count) {
        for (int index = 0; index < modifyCount; index ++) {
            [currentVersionArray addObject:@"0"];
        }
    } else if (lastVersionArray.count < currentVersionArray.count) {
        for (int index = 0; index < modifyCount; index ++) {
            [lastVersionArray addObject:@"0"];
        }
    }
    
    for (int index = 0; index < lastVersionArray.count; index++) {
        if ([currentVersionArray[index] integerValue] > [lastVersionArray[index] integerValue]) {
            return NO;
        } else if ([currentVersionArray[index] integerValue] < [lastVersionArray[index] integerValue]) {
            return YES;
        }
    }
    return NO;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,326评论 25 708
  • 前些天参加一个线下的读书活动,除了我一个大叔外,一群小鲜肉,感觉自己已经被狠狠的拍到了沙滩上。在自我介绍环节,我想...
    佛爷的梦想阅读 2,787评论 0 3
  • 1.先去下载Mac版本SourceTree :下载地址 2.克隆项目到本地,图解如下: 3.检出相应的分支: 4....
    FuWees阅读 864评论 0 4
  • 露天电影 "老少爷们们,今天晚上在大队门口放电影,晚上木事都去看哈。老少爷们们,今天晚上在大队门口……"。每每听到...
    李素问阅读 819评论 0 3
  • 总是离春近的好。花是花,树是树,小草是小草。各有各的颜色,各有各的芳香。微风拂来时,还未长全的树叶发出不成熟的沙沙...
    幸归阅读 351评论 0 0

友情链接更多精彩内容