
Xcode
iOS想要檢查App當(dāng)前版本是否為最新,一般的方案大概都是服務(wù)器自己提供一個(gè)接口來獲取App最新版本是多少,然后再做出相應(yīng)提示是否需要更新,但是接口需要手動(dòng)維護(hù),應(yīng)用要審核,還得等審核通過以后才能更新版本號(hào),其實(shí)蘋果提供了一個(gè)iTunes接口,能夠查到App在AppStore上的狀態(tài)信息,既省事又準(zhǔn)確,下面記錄一下具體實(shí)現(xiàn)方法。
接口信息
- 這是 iTunes 接口地址 ,有興趣可以看一下,我們要用到的接口如下,
xxx處換成自己App的AppId,AppId可以在iTunes Connect里面看到。
http://itunes.apple.com/lookup?id=xxx
- 接口返回的內(nèi)容有很多,我就挑一些有用的截出來了。
{
"resultCount" : 1,
"results" : [{
"artistId" : "開發(fā)者 ID",
"artistName" : "開發(fā)者名稱",
"trackCensoredName" : "審查名稱",
"trackContentRating" : "評(píng)級(jí)",
"trackId" : "應(yīng)用程序 ID",
"trackName" = "應(yīng)用程序名稱",
"trackViewUrl" = "應(yīng)用程序下載網(wǎng)址",
"userRatingCount" = "用戶評(píng)論數(shù)量",
"userRatingCountForCurrentVersion" = "當(dāng)前版本的用戶評(píng)論數(shù)量",
"version" = "版本號(hào)"
}]
}
實(shí)現(xiàn)方法
下面是檢查版本更新的具體實(shí)現(xiàn)方法,注意接口地址 xxx 處換成自己 App 的 AppId ,App 審核的時(shí)候版本肯定是比 AppStore 上高的,所以不用擔(dān)心審核時(shí)會(huì)跳出更新提示。
/// 檢查版本更新
- (void)checkVersion {
NSString *url = @"http://itunes.apple.com/lookup?id=xxx";
[[AFHTTPSessionManager manager] POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
DLog(@"版本更新檢查成功");
NSArray *results = responseObject[@"results"];
if (results && results.count > 0) {
NSDictionary *response = results.firstObject;
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; // 軟件的當(dāng)前版本
NSString *lastestVersion = response[@"version"]; // AppStore 上軟件的最新版本
if (currentVersion && lastestVersion && ![self isLastestVersion:currentVersion compare:lastestVersion]) {
// 給出提示是否前往 AppStore 更新
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"檢測(cè)到有版本更新,是否前往 AppStore 更新版本。" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *trackViewUrl = response[@"trackViewUrl"]; // AppStore 上軟件的地址
if (trackViewUrl) {
NSURL *appStoreURL = [NSURL URLWithString:trackViewUrl];
if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
[[UIApplication sharedApplication] openURL:appStoreURL];
}
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
DLog(@"版本更新檢查失敗");
}];
}
/// 判斷是否最新版本號(hào)(大于或等于為最新)
- (BOOL)isLastestVersion:(NSString *)currentVersion compare:(NSString *)lastestVersion {
if (currentVersion && lastestVersion) {
// 拆分成數(shù)組
NSMutableArray *currentItems = [[currentVersion componentsSeparatedByString:@"."] mutableCopy];
NSMutableArray *lastestItems = [[lastestVersion componentsSeparatedByString:@"."] mutableCopy];
// 如果數(shù)量不一樣補(bǔ)0
NSInteger currentCount = currentItems.count;
NSInteger lastestCount = lastestItems.count;
if (currentCount != lastestCount) {
NSInteger count = labs(currentCount - lastestCount); // 取絕對(duì)值
for (int i = 0; i < count; ++i) {
if (currentCount > lastestCount) {
[lastestItems addObject:@"0"];
} else {
[currentItems addObject:@"0"];
}
}
}
// 依次比較
BOOL isLastest = YES;
for (int i = 0; i < currentItems.count; ++i) {
NSString *currentItem = currentItems[i];
NSString *lastestItem = lastestItems[i];
if (currentItem.integerValue != lastestItem.integerValue) {
isLastest = currentItem.integerValue > lastestItem.integerValue;
break;
}
}
return isLastest;
}
return NO;
}
將來的你,一定會(huì)感激現(xiàn)在拼命的自己,愿自己與讀者的開發(fā)之路無限美好。