對(duì)比版本號(hào)一般是為了實(shí)現(xiàn)版本更新的功能,彈窗提示用戶更新或者直接跳轉(zhuǎn)的App Store。
獲取應(yīng)用在App Store中的版本信息
這里我們使用的是iTunes 的lookup 接口,前提是你已經(jīng)發(fā)布到App Store。
獲取應(yīng)用在App Store中的id
下面是可以獲取到App Store中的id 的兩種方法
- App Store搜索<軟件名稱>,例如:微信,進(jìn)入詳情頁(yè)后,點(diǎn)擊分享按鈕,點(diǎn)擊拷貝鏈接

-
長(zhǎng)按已經(jīng)下載的軟件,點(diǎn)擊分享App,點(diǎn)擊拷貝鏈接 長(zhǎng)按App
找到在App Store這個(gè)鏈接中的idhttps://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124
獲取應(yīng)用在App Store的版本號(hào)
使用AFNetworking的GET請(qǐng)求,請(qǐng)求如下
+ (void)getAppStoreVersionInfoWithResponseBlock:(void (^)(NSDictionary *, NSError *))responseBlock {
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
[sessionManager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];
[sessionManager GET:@"https://itunes.apple.com/cn/lookup?id=414478124" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *dataArray = responseObject[@"results"];
if (dataArray.count != 0) {// 先判斷返回的數(shù)據(jù)是否為空
NSDictionary *dict = dataArray.firstObject;
responseBlock(dict, nil);
} else {
NSError *emptyError = [[NSError alloc] initWithDomain:NSStringFromClass(self) code:NetWorkResponseCodeAppearError userInfo:@{NSLocalizedDescriptionKey:@"內(nèi)容為空"}];
responseBlock(nil, emptyError);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
responseBlock(nil, error);
}];
}
成功后的json
{
"resultCount": 1,
"results": [{
"isGameCenterEnabled": false,
"artworkUrl60": "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/0f/bc/3c/0fbc3c2a-f664-c4e3-bda2-970146031f0e/source/60x60bb.jpg",
"artworkUrl512": "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/0f/bc/3c/0fbc3c2a-f664-c4e3-bda2-970146031f0e/source/512x512bb.jpg",
"artworkUrl100": "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/0f/bc/3c/0fbc3c2a-f664-c4e3-bda2-970146031f0e/source/100x100bb.jpg",
"artistViewUrl": "https://apps.apple.com/cn/developer/wechat/id614694882?uo=4",
"screenshotUrls": ["https://is4-ssl.mzstatic.com/image/thumb/Purple123/v4/98/9e/61/989e614c-0c12-298d-9960-3aa178a88649/pr_source.png/392x696bb.png", "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/83/3c/d0/833cd07d-5b02-8ac1-b59a-fe2f3b17d7f4/pr_source.png/392x696bb.png", "https://is2-ssl.mzstatic.com/image/thumb/Purple113/v4/37/7b/7f/377b7f92-2d20-5fc2-d662-63da28155353/pr_source.png/392x696bb.png", "https://is5-ssl.mzstatic.com/image/thumb/Purple123/v4/35/ac/e2/35ace258-55be-2699-c9be-f8a9532e2aed/pr_source.png/392x696bb.png", "https://is2-ssl.mzstatic.com/image/thumb/Purple123/v4/ac/c1/54/acc15416-9cbf-092d-7342-699afa8874b4/pr_source.png/392x696bb.png"],
"ipadScreenshotUrls": ["https://is4-ssl.mzstatic.com/image/thumb/Purple113/v4/18/8d/b6/188db60e-7d8c-fad3-1a29-0902284945f7/mzl.imkvrcco.jpg/576x768bb.jpg", "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/1e/b6/33/1eb633ef-7b94-2eec-d135-ae9d38b29eb6/mzl.bwmfmxug.jpg/576x768bb.jpg", "https://is3-ssl.mzstatic.com/image/thumb/Purple123/v4/4e/45/89/4e458955-91de-a903-0250-9e56537b4d9d/mzl.gxopfyrb.jpg/576x768bb.jpg"],
"appletvScreenshotUrls": [],
"advisories": ["頻繁/強(qiáng)烈的成人/性暗示題材"],
"supportedDevices": ["Watch4-Watch4", "iPhone5s-iPhone5s", "iPadAir-iPadAir", "iPadAirCellular-iPadAirCellular", "iPadMiniRetina-iPadMiniRetina", "iPadMiniRetinaCellular-iPadMiniRetinaCellular", "iPhone6-iPhone6", "iPhone6Plus-iPhone6Plus", "iPadAir2-iPadAir2", "iPadAir2Cellular-iPadAir2Cellular", "iPadMini3-iPadMini3", "iPadMini3Cellular-iPadMini3Cellular", "iPodTouchSixthGen-iPodTouchSixthGen", "iPhone6s-iPhone6s", "iPhone6sPlus-iPhone6sPlus", "iPadMini4-iPadMini4", "iPadMini4Cellular-iPadMini4Cellular", "iPadPro-iPadPro", "iPadProCellular-iPadProCellular", "iPadPro97-iPadPro97", "iPadPro97Cellular-iPadPro97Cellular", "iPhoneSE-iPhoneSE", "iPhone7-iPhone7", "iPhone7Plus-iPhone7Plus", "iPad611-iPad611", "iPad612-iPad612", "iPad71-iPad71", "iPad72-iPad72", "iPad73-iPad73", "iPad74-iPad74", "iPhone8-iPhone8", "iPhone8Plus-iPhone8Plus", "iPhoneX-iPhoneX", "iPad75-iPad75", "iPad76-iPad76", "iPhoneXS-iPhoneXS", "iPhoneXSMax-iPhoneXSMax", "iPhoneXR-iPhoneXR", "iPad812-iPad812", "iPad834-iPad834", "iPad856-iPad856", "iPad878-iPad878", "iPadMini5-iPadMini5", "iPadMini5Cellular-iPadMini5Cellular", "iPadAir3-iPadAir3", "iPadAir3Cellular-iPadAir3Cellular", "iPodTouchSeventhGen-iPodTouchSeventhGen", "iPhone11-iPhone11", "iPhone11Pro-iPhone11Pro", "iPadSeventhGen-iPadSeventhGen", "iPadSeventhGenCellular-iPadSeventhGenCellular", "iPhone11ProMax-iPhone11ProMax"],
"kind": "software",
"features": ["iosUniversal"],
"trackViewUrl": "https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124?uo=4",
"trackCensoredName": "微信",
"languageCodesISO2A": ["AR", "ZH", "EN", "FR", "DE", "ID", "IT", "JA", "KO", "MS", "PT", "RU", "ZH", "ES", "TH", "ZH", "TR", "VI"],
"fileSizeBytes": "273698816",
"sellerUrl": "http://weixin.qq.com",
"contentAdvisoryRating": "17+",
"averageUserRatingForCurrentVersion": 4.0,
"userRatingCountForCurrentVersion": 74205,
"trackContentRating": "17+",
"currentVersionReleaseDate": "2019-09-25T03:13:08Z",
"releaseNotes": "本次更新:\n- 解決了一些已知問(wèn)題。\n\n最近更新:\n- 可以把收藏筆記、文件預(yù)覽等頁(yè)面設(shè)為浮窗。",
"isVppDeviceBasedLicensingEnabled": true,
"primaryGenreName": "Social Networking",
"genreIds": ["6005", "6007"],
"primaryGenreId": 6005,
"sellerName": "Tencent Technology (Shenzhen) Company Limited",
"releaseDate": "2011-01-21T01:32:15Z",
"trackId": 414478124,
"trackName": "微信",
"formattedPrice": "免費(fèi)",
"minimumOsVersion": "9.0",
"currency": "CNY",
"version": "7.0.8",
"wrapperType": "software",
"artistId": 614694882,
"artistName": "WeChat",
"genres": ["社交", "效率"],
"price": 0.00,
"description": "微信是一款全方位的手機(jī)通訊應(yīng)用,幫助你輕松連接全球好友。微信可以群聊、進(jìn)行視頻聊天、與好友一起玩游戲,以及分享自己的\n生活到朋友圈,讓你感受耳目一新的移動(dòng)生活方式。\n\n 為什么要使用微信:\n ? 多媒體消息:支持發(fā)送視頻、圖片、文本和語(yǔ)音消息。\n ? 群聊和通話:組建高達(dá)500人的群聊和高達(dá)9人的實(shí)時(shí)視頻聊天。\n ? 語(yǔ)音和視頻聊天:提供全球的高質(zhì)量通話。\n ? 表情商店:海量動(dòng)態(tài)表情,包括熱門卡通人物和電影,讓聊天變得更生動(dòng)有趣。\n ? 朋友圈:與好友分享每個(gè)精彩瞬間,記錄自己的生活點(diǎn)滴。\n ? 隱私保護(hù):嚴(yán)格保護(hù)用戶的隱私安全,是唯一一款通過(guò)TRUSTe認(rèn)證的實(shí)時(shí)通訊應(yīng)用。\n ? 認(rèn)識(shí)新朋友:通過(guò)“雷達(dá)加朋友”、“附近的人”和“搖一搖”認(rèn)識(shí)新朋友。\n ? 實(shí)時(shí)位置共享:與好友分享地理位置,無(wú)需通過(guò)語(yǔ)言告訴對(duì)方。\n ? 多語(yǔ)言:支持超過(guò)20種語(yǔ)言界面,并支持多國(guó)語(yǔ)言的消息翻譯。\n ? 微信運(yùn)動(dòng):支持接入Apple Watch及健康app數(shù)據(jù),可在步數(shù)排行榜上和朋友一較高下。若需使用,可在“設(shè)置-通用-輔助功能”內(nèi)啟用。 \n ? 更多功能: 支持跨平臺(tái)、聊天室墻紙自定義、消息提醒自定義和公眾號(hào)服務(wù)等。",
"bundleId": "com.tencent.xin",
"averageUserRating": 4.5,
"userRatingCount": 5041979
}]
}
可以先用Postman先調(diào)用下,然后在根據(jù)自己的需要來(lái)寫數(shù)據(jù)模型。獲取到結(jié)果后,可以直接取出App Store中的版本號(hào)"version": "7.0.8"。
本地版本號(hào)與App Store版本號(hào)的對(duì)比
拿微信舉例,任何版本(低于、高于、等于App Store版本)點(diǎn)擊版本更新按鈕都會(huì)跳到App Store,我的做法則是做個(gè)版本信息的彈窗。
版本號(hào)盡量使用三個(gè)以內(nèi)句點(diǎn)分隔的,使用規(guī)則可以參考我之后的文章《iOS 蘋果禁止使用超過(guò)三個(gè)句點(diǎn)分隔的版本號(hào)(Bundle Versions)》
獲取本地的版本號(hào)
- 獲取App信息,需要取出Info.plist中包含的信息。
//A dictionary, constructed from the bundle’s Info.plist file, that contains information about the receiver.
- (NSDictionary<NSString *, id> *)appInfoDictionary {
return [[NSBundle mainBundle] infoDictionary];
}
- 取出版本號(hào)
- (NSString *)appVersion {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}
對(duì)比
版本號(hào)大都是
1.0.0這樣的,所以需要把每一位都取出來(lái)進(jìn)行對(duì)比。
我原來(lái)使用的是for-in循環(huán),參考了QMUIKit中 + compareSystemVersion: toVersion:方法后,覺(jué)得while循環(huán)更適合,代碼如下
+ (NSComparisonResult)compareSystemVersion:(NSString *)currentVersion toVersion:(NSString *)targetVersion {
NSArray *currentVersionArr = [currentVersion componentsSeparatedByString:@"."];
NSArray *targetVersionArr = [targetVersion componentsSeparatedByString:@"."];
NSInteger pos = 0;
while ([currentVersionArr count] > pos || [targetVersionArr count] > pos) {
NSInteger v1 = [currentVersionArr count] > pos ? [[currentVersionArr objectAtIndex:pos] integerValue] : 0;
NSInteger v2 = [targetVersionArr count] > pos ? [[targetVersionArr objectAtIndex:pos] integerValue] : 0;
if (v1 < v2) {
return NSOrderedAscending;
}
else if (v1 > v2) {
return NSOrderedDescending;
}
pos++;
}
return NSOrderedSame;
}
版本更新完整代碼
- (void)p_versionUpdateButtonAction {
//獲取本地的App版本號(hào)
NSString *appVersion = [VPUtilityTool sharedInstance].appVersion;
[VPMineRequestHandler getAppStoreVersionInfoWithResponseBlock:^(NSDictionary * _Nonnull response, NSError * _Nonnull error) {
VPLog(@"%@",response);
if (error) {
//跳轉(zhuǎn)到App Store詳情頁(yè)中
[UIApplicationInstance vp_openUrlString:@"https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124"];
} else {
NSString *appStoreVersion = response[@"version"];
NSComparisonResult comparisonResult = [QMUIHelper compareSystemVersion:appVersion toVersion:appStoreVersion];
只要在高于本地版本的結(jié)果下才彈窗
if (comparisonResult == NSOrderedAscending) {
//更新彈窗
[AppAlertViewController showWithTitle:@"版本更新" content:[NSString stringWithFormat:@"發(fā)現(xiàn)最新版本:V%@\n請(qǐng)到App Store下載更新!", appStoreVersion] cancelButtonTitle:@"取消" confirmButtonTitle:@"更新" actionsHandlerBlock:^(BOOL isCancel) {
if (!isCancel) {
[UIApplicationInstance vp_openUrlString:@"https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124"];
}
}];
} else {
[UIApplicationInstance vp_openUrlString:@"https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124"];
}
}
}];
}
版本更新的功能大概就是以上這么多。
