iOS - 版本號(hào)比較

//對(duì)外版本
#define APP_ShortVersion [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]

//對(duì)內(nèi)版本
#define APP_Verson [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"]

對(duì)于iOS版本號(hào)的比較,首先對(duì)兩個(gè)字段要了解,CFBundleShortVersionString是App Store所使用的版本號(hào),也就是你在App Store里能看到的版本號(hào),而CFBundleVersion則是對(duì)開發(fā)團(tuán)隊(duì)比較有意義的一個(gè)編號(hào),可用于記錄APP的開發(fā)記錄,便于對(duì)APP的研發(fā)過程有一個(gè)較為系統(tǒng)的了解,比如版本號(hào)為1.0.1,build號(hào)最開始為1.0.1.0,新增某功能或優(yōu)化后,build可改為為1.0.1.1,然后開發(fā)日志上記錄,1.0.1.1,新增XXX,優(yōu)化XXX.

以下是比較方法

//版本號(hào)判斷
- (BOOL)localCompareWithAppStoreVerson:(NSString *)appVerson {
???
??? BOOL compareResult = NO;
???
??? //版本號(hào)獲取
??? NSMutableString *localString = [NSMutableString new];
??? NSMutableString *appStoreString = [NSMutableString new];
???
??? if (self.length != appVerson.length) {
??????? // 獲取各個(gè)版本號(hào)對(duì)應(yīng)版本信息
??????? NSMutableArray *localArray = [NSMutableArray arrayWithArray:[self componentsSeparatedByString:@"."]];
??????? NSMutableArray *appStoreArray = [NSMutableArray arrayWithArray:[appVerson componentsSeparatedByString:@"."]];
???????
??????? // 補(bǔ)全版本信息為相同位數(shù)
??????? while (localArray.count < appStoreArray.count) {
??????????? [localArray addObject:@"0"];
??????? }
??????? while (appStoreArray.count < localArray.count) {
??????????? [appStoreArray addObject:@"0"];
??????? }
???????
??????? //版本號(hào)重組
??????? [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];
??? }
???
??? //對(duì)比版本號(hào)
??? NSComparisonResult compare = [appStoreString compare:localString options:NSNumericSearch];
??? if (compare == NSOrderedDescending) {
??????? //應(yīng)用商店應(yīng)用版本號(hào)高于本地應(yīng)用版本號(hào)
??????? compareResult = YES;
??? }
???
??? return compareResult;
???
}

我是寫了一個(gè)對(duì)字符串的擴(kuò)展,所以可以直接使用字符串調(diào)用改方法,其中self指的是APP當(dāng)前的版本號(hào),appVerson指得是查詢到的App Store里的版本號(hào).

PS: App Store版本號(hào)查詢,調(diào)用接口 "http://itunes.apple.com/cn/lookup?id=XXX",,,其中XXX為要查詢的APPID,然后解析數(shù)據(jù)

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

? ? ? ? NSDictionary *dic = [array firstObject];

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容