对比版本号一般是为了实现版本更新的功能,弹窗提示用户更新或者直接跳转的App Store。
获取应用在App Store中的版本信息
这里我们使用的是iTunes 的lookup 接口,前提是你已经发布到App Store。
获取应用在App Store中的id
下面是可以获取到App Store中的id 的两种方法
- App Store搜索<软件名称>,例如:微信,进入详情页后,点击分享按钮,点击拷贝链接
-
长按已经下载的软件,点击分享App,点击拷贝链接
找到在App Store这个链接中的idhttps://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124
获取应用在App Store的版本号
使用AFNetworking的GET请求,请求如下
+ (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) {// 先判断返回的数据是否为空
NSDictionary *dict = dataArray.firstObject;
responseBlock(dict, nil);
} else {
NSError *emptyError = [[NSError alloc] initWithDomain:NSStringFromClass(self) code:NetWorkResponseCodeAppearError userInfo:@{NSLocalizedDescriptionKey:@"内容为空"}];
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": ["频繁/强烈的成人/性暗示题材"],
"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- 解决了一些已知问题。\n\n最近更新:\n- 可以把收藏笔记、文件预览等页面设为浮窗。",
"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": "免费",
"minimumOsVersion": "9.0",
"currency": "CNY",
"version": "7.0.8",
"wrapperType": "software",
"artistId": 614694882,
"artistName": "WeChat",
"genres": ["社交", "效率"],
"price": 0.00,
"description": "微信是一款全方位的手机通讯应用,帮助你轻松连接全球好友。微信可以群聊、进行视频聊天、与好友一起玩游戏,以及分享自己的\n生活到朋友圈,让你感受耳目一新的移动生活方式。\n\n 为什么要使用微信:\n • 多媒体消息:支持发送视频、图片、文本和语音消息。\n • 群聊和通话:组建高达500人的群聊和高达9人的实时视频聊天。\n • 语音和视频聊天:提供全球的高质量通话。\n • 表情商店:海量动态表情,包括热门卡通人物和电影,让聊天变得更生动有趣。\n • 朋友圈:与好友分享每个精彩瞬间,记录自己的生活点滴。\n • 隐私保护:严格保护用户的隐私安全,是唯一一款通过TRUSTe认证的实时通讯应用。\n • 认识新朋友:通过“雷达加朋友”、“附近的人”和“摇一摇”认识新朋友。\n • 实时位置共享:与好友分享地理位置,无需通过语言告诉对方。\n • 多语言:支持超过20种语言界面,并支持多国语言的消息翻译。\n • 微信运动:支持接入Apple Watch及健康app数据,可在步数排行榜上和朋友一较高下。若需使用,可在“设置-通用-辅助功能”内启用。 \n • 更多功能: 支持跨平台、聊天室墙纸自定义、消息提醒自定义和公众号服务等。",
"bundleId": "com.tencent.xin",
"averageUserRating": 4.5,
"userRatingCount": 5041979
}]
}
可以先用Postman先调用下,然后在根据自己的需要来写数据模型。获取到结果后,可以直接取出App Store中的版本号"version": "7.0.8"
。
本地版本号与App Store版本号的对比
拿微信举例,任何版本(低于、高于、等于App Store版本)点击版本更新按钮都会跳到App Store,我的做法则是做个版本信息的弹窗。
版本号尽量使用三个以内句点分隔的,使用规则可以参考我之后的文章《iOS 苹果禁止使用超过三个句点分隔的版本号(Bundle Versions)》
获取本地的版本号
- 获取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];
}
- 取出版本号
- (NSString *)appVersion {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}
对比
版本号大都是
1.0.0
这样的,所以需要把每一位都取出来进行对比。
我原来使用的是for-in
循环,参考了QMUIKit中 + compareSystemVersion: toVersion:
方法后,觉得while
循环更适合,代码如下
+ (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版本号
NSString *appVersion = [VPUtilityTool sharedInstance].appVersion;
[VPMineRequestHandler getAppStoreVersionInfoWithResponseBlock:^(NSDictionary * _Nonnull response, NSError * _Nonnull error) {
VPLog(@"%@",response);
if (error) {
//跳转到App Store详情页中
[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];
只要在高于本地版本的结果下才弹窗
if (comparisonResult == NSOrderedAscending) {
//更新弹窗
[AppAlertViewController showWithTitle:@"版本更新" content:[NSString stringWithFormat:@"发现最新版本:V%@\n请到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"];
}
}
}];
}
版本更新的功能大概就是以上这么多。