Xcode
iOS
想要檢查App
當(dāng)前版本是否為最新,一般的方案大概都是服務(wù)器自己提供一個接口來獲取App
最新版本是多少肉渴,然后再做出相應(yīng)提示是否需要更新阐污,但是接口需要手動維護(hù)溺忧,應(yīng)用要審核融欧,還得等審核通過以后才能更新版本號敏弃,其實(shí)蘋果提供了一個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" : "評級",
"trackId" : "應(yīng)用程序 ID",
"trackName" = "應(yīng)用程序名稱",
"trackViewUrl" = "應(yīng)用程序下載網(wǎng)址",
"userRatingCount" = "用戶評論數(shù)量",
"userRatingCountForCurrentVersion" = "當(dāng)前版本的用戶評論數(shù)量",
"version" = "版本號"
}]
}
實(shí)現(xiàn)方法
下面是檢查版本更新的具體實(shí)現(xiàn)方法廓啊,注意接口地址 xxx
處換成自己 App
的 AppId
,App
審核的時候版本肯定是比 AppStore
上高的封豪,所以不用擔(dān)心審核時會跳出更新提示谴轮。
/// 檢查版本更新
- (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:@"檢測到有版本更新,是否前往 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(@"版本更新檢查失敗");
}];
}
/// 判斷是否最新版本號(大于或等于為最新)
- (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); // 取絕對值
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;
}
將來的你第步,一定會感激現(xiàn)在拼命的自己,愿自己與讀者的開發(fā)之路無限美好缘琅。