美文网首页
ios -版本号判断

ios -版本号判断

作者: 命运建筑师fly | 来源:发表于2017-09-15 17:23 被阅读0次

需求:

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;
}

相关文章

  • iOS 版本号判断

    NSDictionary *infoDict = [[NSBundle mainBundle] infoDicti...

  • ios -版本号判断

    需求: app升级,需要对比本地和线上的版本号 实现方法: 方法一、利用系统自带方法实现 方法二、规定最大位数,不...

  • iOS 版本判断

    苹果新出的版本号判断,不用在获取当前设备了。 iOS 11 scrollView偏移64px 判断当前系统大于等于...

  • ios系统版本判断

    ios10.0之后执行相关代码,可以用此判断 或者直接获取手机的系统版本号作为判断

  • iOS 版本号大小判断(升级判断)

    需要做升级判断,or 要用到审核开关,就需要用到版本号的大小判断。(之后找篇文章写写iOS审核开关的事情<有点风...

  • ios判断系统版本号

    1.直接获取系统版本号 NSString *version= [UIDevice currentDevice].s...

  • iOS版本号比较判断

    在进行版本更新的时候,需要对当前版本和接口数据版本号就行判断比较,发现有多种的比较方式。 最简单的比较方式,采用字...

  • iOS笔记--UIApplication判断版本号

  • 手动修改Info.plist

    iOS 修改app 版本号和 build 版本号 其他示例

  • iOS10中系统版本的判断

    在iOS10中,当需要判断系统版本号的时候,不能再用以下这种方法了: #define isiOS10 ([[[[U...

网友评论

      本文标题:ios -版本号判断

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