iOS开发之获取App Store的上架信息(包括版本号)并实现检查更新
由于iOS app不允许检查更新,凡是app在上架审核时出现“检查更新”字样的都有可能被打回,而检查更新功能是产品经理最念念不忘的重要功能(内心一万个草泥马。。。),作为攻城狮的我们自然要想方设法实现这一功能。
既然ios在审核时不允许检查更新,那我们何不抓住审核漏洞,也即在审核时不出现检查更新而在实际使用时出现呢。答案就是获取当前app在App Store上的版本信息,并将此版本号与提交审核的app作对比,由于App Store上的版本号必然低于每次提交审核(新版本)的版本号,那么app在审核过程中不可能出现“检查更新”字样,此时用户在使用比App Store上的版本低的app时则相应会提示有新版本更新,并让用户选择跳转App Store进行更新。那么废话不多说,上代码:
(void)requestLookup {
[YTHttpTool POST:YTSystemToolAppStoreLookupURL params:@{@"id": kAppStoreId} success:^(NSURLSessionDataTask *task, id responseObject) {
NSArray *resultArr = responseObject[@"results"];
NSDictionary *resultDic = resultArr.firstObject;
NSString *appCurrentVersion = [YTSystemTool getAppVersion];
NSString *appStoreVersion = resultDic[@"version"];
if ([appCurrentVersion compare:appStoreVersion options:NSNumericSearch] == NSOrderedAscending) {
UINavigationController *rootNC = (UINavigationController *)self.window.rootViewController;
UIViewController *rootVC = rootNC.viewControllers.firstObject;
[YTAlertTool alertTwoActionWithVC:rootVC title:[NSString stringWithFormat:@"您有新版本更新(%@)", appStoreVersion] message:@"" cancelTitle:@"我再看看" cancelHandler:nil defaultTitle:@"现在更新" defaultHandler:^(UIAlertAction * _Nonnull action) {
[YTSystemTool jumpToAppStoreApp];
} completion:nil];
}
} fail:nil];
}
该段代码是写在APPDelegate.m文件中用于检查更新的私有方法,YTHttpTool类是我封装的网络请求类,YTAlertTool是我封装的提示框类,YTSystemTool是我封装的各种系统工具类,此处使用的跳转App Store的工具方法,但是重点在于YTSystemToolAppStoreLookupURL和kAppStoreId这两个常量。YTSystemToolAppStoreLookupURL是请求查询app在App Store的上架信息,固定为@"https://itunes.apple.com/lookup",官方文档说明。需要注意的是旧版本的请求网址是@"http://itunes.apple.com/lookup",自从苹果在iOS 10后使用了https协议,网址便改成了https,以前那些类似的博客均为http;kAppStoreId为app上架App Store时分配每个app的id,例如我的上架app是@"1197227551",获取方法见下图:
触摸板右键选择“拷贝链接”得到当前app在App Store上的完整网址,如我的是:https://itunes.apple.com/us/app/众驾租车/id1197227551?mt=8,截取其中的id即可。至此,调用查询请求的完整URL(POST请求)为https://itunes.apple.com/lookup?id=1197227551,请求url后台打印结果如下:
url:https://itunes.apple.com/lookup params:{
id = 1197227551;
} response:{
resultCount = 1;
results = (
{
advisories = (
);
appletvScreenshotUrls = (
);
artistId = 1197227550;
artistName = "\U6b66\U6c49\U55bb\U5929\U79d1\U6280\U6709\U9650\U516c\U53f8";
artistViewUrl = "https://itunes.apple.com/us/developer/武汉喻天科技有限公司/id1197227550?uo=4";
artworkUrl100 = "http://is1.mzstatic.com/image/thumb/Purple111/v4/d8/93/9f/d8939fc1-c834-b461-aafb-d87de4b549b6/source/100x100bb.jpg";
artworkUrl512 = "http://is1.mzstatic.com/image/thumb/Purple111/v4/d8/93/9f/d8939fc1-c834-b461-aafb-d87de4b549b6/source/512x512bb.jpg";
artworkUrl60 = "http://is1.mzstatic.com/image/thumb/Purple111/v4/d8/93/9f/d8939fc1-c834-b461-aafb-d87de4b549b6/source/60x60bb.jpg";
bundleId = "com.YTTimeRent.027hb";
contentAdvisoryRating = "4+";
currency = USD;
currentVersionReleaseDate = "2017-03-03T02:55:24Z";
description = "\U4f17\U9a7e\U79df\U8f66\U4e13\U6ce8\U4e8e\U65b0\U80fd\U6e90\U6c7d\U8f66\U7684\U5206\U65f6\U79df\U8d41\Uff0c\U81f4\U529b\U4e8e\U901a\U8fc7\U7535\U52a8\U6c7d\U8f66\U5171\U4eab\U6765\U89e3\U51b3\U57ce\U5e02\U51fa\U884c\U96be\U9898\Uff0c\U4ee5\U79fb\U52a8\U4e92\U8054\U7f51\U4e3a\U57fa\U7840\Uff0c\U7ed3\U5408\U5404\U9879\U804c\U80fd\U6280\U672f\Uff0c\U6765\U5b9e\U73b0\U7528\U6237\U5feb\U901f\U627e\U8f66\Uff0cAPP\U5f00\U95e8\U5173\U95e8\Uff0c\U8fd8\U8f66\U7684\U5168\U7a0b\U81ea\U52a8\U5316\U3002\U6309\U7167\U65f6\U95f4\U7684\U8ba1\U8d39\U65b9\U5f0f\Uff0c\U4e3a\U7528\U6237\U63d0\U4f9b\U7eff\U8272\U3001\U7ecf\U6d4e\U3001\U4fbf\U6377\U7684\U51fa\U884c\U89e3\U51b3\U65b9\U6848\U3002";
features = (
);
fileSizeBytes = 46412800;
formattedPrice = Free;
genreIds = (
6003
);
genres = (
Travel
);
ipadScreenshotUrls = (
);
isGameCenterEnabled = 0;
isVppDeviceBasedLicensingEnabled = 1;
kind = software;
languageCodesISO2A = (
ZH
);
minimumOsVersion = "8.0";
price = 0;
primaryGenreId = 6003;
primaryGenreName = Travel;
releaseDate = "2017-02-03T17:20:52Z";
releaseNotes = "(\U4fee\U6539)\U4fee\U6539\U7528\U6237\U534f\U8bae\U5185\U5bb9\n(\U4f18\U5316)\U4f18\U5316\U8d26\U5355\U754c\U9762\U4ee3\U7801\n(\U4fee\U590d)\U4fee\U590d\U4f18\U60e0\U5238\U4e0d\U53ef\U7528\U95ee\U9898\n(\U4f18\U5316)\U4f18\U5316\U641c\U7d22\U6846\n(\U589e\U52a0)\U5730\U56fe\U754c\U9762\U8fdb\U5165\U540e\U53f0\U65b0\U589e\U524d\U53f0\U81ea\U52a8\U5237\U65b0\U529f\U80fd";
screenshotUrls = (
"http://a2.mzstatic.com/us/r30/Purple122/v4/87/32/4f/87324f3a-d512-9c42-db11-3535b42d71c2/screen696x696.jpeg",
"http://a4.mzstatic.com/us/r30/Purple111/v4/9d/6e/93/9d6e93a5-651d-b6dc-078f-29f4f1d3e307/screen696x696.jpeg",
"http://a3.mzstatic.com/us/r30/Purple122/v4/2e/da/6c/2eda6c1e-c87e-f907-a414-1a7e31ec969e/screen696x696.jpeg",
"http://a2.mzstatic.com/us/r30/Purple122/v4/12/0e/af/120eaf86-a637-b9d8-5299-46087bbeabe2/screen696x696.jpeg"
);
sellerName = "YOU-TURN TECHNOLOGY CO.,LTD";
sellerUrl = "http://www.027hb.cn/";
supportedDevices = (
"iPad2Wifi-iPad2Wifi",
"iPad23G-iPad23G",
"iPhone4S-iPhone4S",
"iPadThirdGen-iPadThirdGen",
"iPadThirdGen4G-iPadThirdGen4G",
"iPhone5-iPhone5",
"iPodTouchFifthGen-iPodTouchFifthGen",
"iPadFourthGen-iPadFourthGen",
"iPadFourthGen4G-iPadFourthGen4G",
"iPadMini-iPadMini",
"iPadMini4G-iPadMini4G",
"iPhone5c-iPhone5c",
"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"
);
trackCensoredName = "\U4f17\U9a7e\U79df\U8f66";
trackContentRating = "4+";
trackId = 1197227551;
trackName = "\U4f17\U9a7e\U79df\U8f66";
trackViewUrl = "https://itunes.apple.com/us/app/众驾租车/id1197227551?mt=8&uo=4";
version = "1.0.9";
wrapperType = software;
}
);
}
截取其中字段”version“即获取当前在App Store的版本号。
下面介绍获取当前app提交审核的版本号,在YTSystemTool.m文件中的代码如下:
+ (NSString *)getAppVersion {
NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];
NSString *appCurVersion = [infoDic objectForKey:kAppCurrentVersion];
return appCurVersion;
}
其中kAppCurrentVersion表示info.plist文件中版本号的字段名,为固定常量@"CFBundleShortVersionString"
获取后将两个版本号进行比较,比较过程调用了系统NSString的API:- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask,该方法可以比较任意版本号的大小,方法返回比较结果枚举NSComparisonResult。
比较完成后弹出版本更新提示框,如下图:
接下来介绍更新跳转的代码实现:同样是在YTSystemTool.m文件中,代码如下:
+ (void)jumpToAppStoreApp {
NSString *appStoreURLStr = kAppStoreURLString;
NSURL *appStoreURL = [NSURL URLWithString:appStoreURLStr];
if ([[UIApplication sharedApplication]canOpenURL:appStoreURL]) {
if ([UIDevice currentDevice].systemVersion.doubleValue >= 10.0) {
[[UIApplication sharedApplication] openURL:appStoreURL options:@{UIApplicationOpenURLOptionsAnnotationKey: @"YES"} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:appStoreURL];
}
}
}
其中kAppStoreURLString表示App Store应用间通信的URL字符串常量,这里为@"itms-apps://itunes.apple.com/us/app/zhong-jia-zu-che/id1197227551?mt=8",打开iTunes应用,搜索当前app获取方法如下图:
点击上图中的尖尖号,并在弹出的视图中选择Copy Link,如图:
我获取到的URL为 https://itunes.apple.com/us/app/众驾租车/id1197227551?mt=8,此时将“https”替换为“itms-apps”即可实现跳转。