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

你可能感兴趣的:(ios -版本号判断)