iOS - 版本号比较

//对外版本
#define APP_ShortVersion [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]

//对内版本
#define APP_Verson [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"]

对于iOS版本号的比较,首先对两个字段要了解,CFBundleShortVersionString是App Store所使用的版本号,也就是你在App Store里能看到的版本号,而CFBundleVersion则是对开发团队比较有意义的一个编号,可用于记录APP的开发记录,便于对APP的研发过程有一个较为系统的了解,比如版本号为1.0.1,build号最开始为1.0.1.0,新增某功能或优化后,build可改为为1.0.1.1,然后开发日志上记录,1.0.1.1,新增XXX,优化XXX.

以下是比较方法

//版本号判断
- (BOOL)localCompareWithAppStoreVerson:(NSString *)appVerson {
   
    BOOL compareResult = NO;
   
    //版本号获取
    NSMutableString *localString = [NSMutableString new];
    NSMutableString *appStoreString = [NSMutableString new];
   
    if (self.length != appVerson.length) {
        // 获取各个版本号对应版本信息
        NSMutableArray *localArray = [NSMutableArray arrayWithArray:[self componentsSeparatedByString:@"."]];
        NSMutableArray *appStoreArray = [NSMutableArray arrayWithArray:[appVerson componentsSeparatedByString:@"."]];
       
        // 补全版本信息为相同位数
        while (localArray.count < appStoreArray.count) {
            [localArray addObject:@"0"];
        }
        while (appStoreArray.count < localArray.count) {
            [appStoreArray addObject:@"0"];
        }
       
        //版本号重组
        [localString appendString:[localArray firstObject]];
        [appStoreString appendString:[appStoreArray firstObject]];
       
        for (int i = 1; i < localArray.count; i++) {
            [localString appendString:@"."];
            [localString appendString:[localArray objectAtIndex:i]];
        }
        for (int i = 1; i < appStoreArray.count; i++) {
            [appStoreString appendString:@"."];
            [appStoreString appendString:[appStoreArray objectAtIndex:i]];
        }
       
    } else {
        localString = [NSMutableString stringWithString:self];
        appStoreString = [NSMutableString stringWithString:appVerson];
    }
   
    //对比版本号
    NSComparisonResult compare = [appStoreString compare:localString options:NSNumericSearch];
    if (compare == NSOrderedDescending) {
        //应用商店应用版本号高于本地应用版本号
        compareResult = YES;
    }
   
    return compareResult;
   
}

我是写了一个对字符串的扩展,所以可以直接使用字符串调用改方法,其中self指的是APP当前的版本号,appVerson指得是查询到的App Store里的版本号.

PS: App Store版本号查询,调用接口 "http://itunes.apple.com/cn/lookup?id=XXX",,,其中XXX为要查询的APPID,然后解析数据

NSArray *array = [responseObject objectForKey:@"results"];

        NSDictionary *dic = [array firstObject];

        NSString *appStoreVersion = [NSString stringWithFormat:@"%@",[dic objectForKey:@"version"]];

你可能感兴趣的:(iOS - 版本号比较)