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 = "U6b66U6c49U55bbU5929U79d1U6280U6709U9650U516cU53f8";
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 = "U4f17U9a7eU79dfU8f66U4e13U6ce8U4e8eU65b0U80fdU6e90U6c7dU8f66U7684U5206U65f6U79dfU8d41Uff0cU81f4U529bU4e8eU901aU8fc7U7535U52a8U6c7dU8f66U5171U4eabU6765U89e3U51b3U57ceU5e02U51faU884cU96beU9898Uff0cU4ee5U79fbU52a8U4e92U8054U7f51U4e3aU57faU7840Uff0cU7ed3U5408U5404U9879U804cU80fdU6280U672fUff0cU6765U5b9eU73b0U7528U6237U5febU901fU627eU8f66Uff0cAPPU5f00U95e8U5173U95e8Uff0cU8fd8U8f66U7684U5168U7a0bU81eaU52a8U5316U3002U6309U7167U65f6U95f4U7684U8ba1U8d39U65b9U5f0fUff0cU4e3aU7528U6237U63d0U4f9bU7effU8272U3001U7ecfU6d4eU3001U4fbfU6377U7684U51faU884cU89e3U51b3U65b9U6848U3002";
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 = "(U4feeU6539)U4feeU6539U7528U6237U534fU8baeU5185U5bb9 (U4f18U5316)U4f18U5316U8d26U5355U754cU9762U4ee3U7801 (U4feeU590d)U4feeU590dU4f18U60e0U5238U4e0dU53efU7528U95eeU9898 (U4f18U5316)U4f18U5316U641cU7d22U6846 (U589eU52a0)U5730U56feU754cU9762U8fdbU5165U540eU53f0U65b0U589eU524dU53f0U81eaU52a8U5237U65b0U529fU80fd";
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 = "U4f17U9a7eU79dfU8f66";
trackContentRating = "4+";
trackId = 1197227551;
trackName = "U4f17U9a7eU79dfU8f66";
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”即可实现跳转。

原文地址:https://www.cnblogs.com/keqipu/p/6511021.html