【iOS 開發(fā)】利用 iTunes 接口檢查 App 版本更新

Xcode

iOS 想要檢查 App 當(dāng)前版本是否為最新,一般的方案大概都是服務(wù)器自己提供一個(gè)接口來獲取 App 最新版本是多少,然后再做出相應(yīng)提示是否需要更新,但是接口需要手動(dòng)維護(hù),應(yīng)用要審核,還得等審核通過以后才能更新版本號(hào),其實(shí)蘋果提供了一個(gè) iTunes 接口,能夠查到 AppAppStore 上的狀態(tài)信息,既省事又準(zhǔn)確,下面記錄一下具體實(shí)現(xiàn)方法。


接口信息

  • 這是 iTunes 接口地址 ,有興趣可以看一下,我們要用到的接口如下,xxx 處換成自己 AppAppId ,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 處換成自己 AppAppId ,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ā)之路無限美好。

我的傳送門: 博客 、簡(jiǎn)書 、微博 、GitHub 。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容