iOS App版本号比对

//比对版本号
//NSOrderedAscending -1   输入版本号大于app版本号
//NSOrderedSame       0   输入版本号等于app版本号
//NSOrderedDescending 1   输入版本号小于app版本号
- (NSComparisonResult)compare:(NSString *)version {
    //当前版本
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    NSLog(@"当前app版本%@,比对的版本%@",appVersion,version);
    
    NSArray *appVersionArr = [appVersion componentsSeparatedByString:@"."];
    NSArray *versionArr = [version componentsSeparatedByString:@"."];

    if ([appVersionArr[0] integerValue] > [versionArr[0] integerValue]) {
        return NSOrderedDescending;
    }else if (([appVersionArr[0] integerValue] == [versionArr[0] integerValue]) && ([appVersionArr[1] integerValue] > [versionArr[1] integerValue])) {
        return NSOrderedDescending;
    }else if (([appVersionArr[0] integerValue] == [versionArr[0] integerValue]) && ([appVersionArr[1] integerValue] == [versionArr[1] integerValue]) && ([appVersionArr[2] integerValue] > [versionArr[2] integerValue])) {
        return NSOrderedDescending;
    }else if (([appVersionArr[0] integerValue] == [versionArr[0] integerValue]) && ([appVersionArr[1] integerValue] == [versionArr[1] integerValue]) && ([appVersionArr[2] integerValue] == [versionArr[2] integerValue])) {
        return NSOrderedSame;
    }
    return NSOrderedAscending;
}


你可能感兴趣的:(iOS App版本号比对)