您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS App 升級檢測:比對 App Store 版號並跳轉下載

編輯:iOS 開發綜合

想主動提醒使用者「有新版本」,最可靠的方式不是自己猜,而是去問 App Store。做法是呼叫 iTunes 的 lookup 介面取得線上版號,跟本地 bundle 的版本比大小,若線上較新就彈出提示。

一、送出非同步請求

/**
 * 檢測軟體是否需要升級
 */
-(void)checkVersion
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
        @"https://itunes.apple.com/cn/lookup?id=%i", iFeverAPPID]];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    [request setUseCookiePersistence:YES];
    [request setDelegate:self];
    [request setDidFailSelector:@selector(getVersionRequestFailed:)];
    [request setDidFinishSelector:@selector(getVersionRequestSuccess:)];
    [request startAsynchronous];
}

-(void)getVersionRequestFailed:(ASIHTTPRequest *)request1
{
    NSLog(@"從 AppStore 獲取版本信息失敗!!");
}

二、解析回應並比對版號

-(void)getVersionRequestSuccess:(ASIHTTPRequest *)request1
{
    NSString *newVersion = nil;
    NSData *responseData = [request1 responseData];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData
                                                       options:NSJSONReadingAllowFragments
                                                         error:nil];
    NSArray *resultArray = [dic objectForKey:@"results"];
    for (id config in resultArray) {
        newVersion = [config valueForKey:@"version"];
    }

    if (newVersion) {
        NSLog(@"通過 AppStore 獲取的版本號是:%@", newVersion);
    }

    // 取得本地版本號
    NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSString *msg = [NSString stringWithFormat:
        @"你當前的版本是 V%@,發現新版本 V%@,是否下載新版本?", localVersion, newVersion];

    if ([newVersion floatValue] > [localVersion floatValue]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"升級提示!"
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"下次再說"
                                              otherButtonTitles:@"現在升級", nil];
        alert.tag = kVersionNeedUpdateAlertTag;
        [alert show];
    }
}

三、使用者按下「現在升級」

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == kVersionNeedUpdateAlertTag) {
        if (buttonIndex == 1) {
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
                @"https://itunes.apple.com/cn/app/id%i?mt=8", iFeverAPPID]];
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

另一種寫法是直接開 itms-apps:// 開頭的 iTunes 連結,但實測常常跳出「無法連接到 iTunes」的提示,不太建議使用。

想先看回應長什麼樣子?

把 App ID 代入這個網址就能直接在瀏覽器看到 JSON:

https://itunes.apple.com/cn/lookup?id=465039730

幾點現代化建議

  • 不要用浮點數比版號。floatValue 對「1.10」與「1.9」會判錯,因為 1.10 反而小於 1.9。請改用 compare:options:NSNumericSearch 逐段比對。
  • 記得檢查 App 所屬地區。lookup 網址帶有國家代碼,海外上架的 App 要抓對應 storefront,否則可能查不到。
  • 用 https 並做好快取。這個請求每次啟動都打太耗流量,建議加上「距上次檢查超過 N 小時才查詢」的節流邏輯,並記得處理無網路的情況。
  • ASIHTTPRequest 已停止維護,新專案請改用 NSURLSession。UIAlertView 在 iOS 9 之後也已被 UIAlertController 取代。
  • 若你的 App 有 frequent 更新節奏,也可以考慮改用 Apple 提供的 SKStoreProductViewController,使用者能直接在 App 內完成更新,不必跳出到 App Store。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved