iOS通過iTunes search檢測版本更新,並提示用戶更新!

如果我們要檢測app版本的更新,那麼我們必須獲取當前運行app版本的版本信息和appstore 上發佈的最新版本的信息。

當前運行版本信息可以通過info.plist文件中的bundle version中獲取:

  1. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
  2.     CFShow(infoDic);  
  3.       
  4.     NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];  

這樣就獲取到當前運行的app的版本了


要獲取當前app store上的最新的版本,有兩種方法,

一、在某特定的服務器上,發佈和存儲app最新的版本信息,需要的時候向該服務器請求查詢。

二、從app store上查詢,可以獲取到app的作者,連接,版本等。官方相關文檔

www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.htm


具體步驟如下:
1,用 POST 方式發送請求:
http://itunes.apple.com/search?term=你的應用程序名稱&entity=software
更加精準的做法是根據 app 的 id 來查找:
http://itunes.apple.com/lookup?id=你的應用程序的ID

#define APP_URL http://itunes.apple.com/lookup?id=你的應用程序的ID

你的應用程序的ID 是 itunes connect裏的 Apple ID

2,從獲得的 response 數據中解析需要的數據。因爲從 appstore 查詢得到的信息是 JSON 格式的,所以需要經過解析。解析之後得到的原始數據就是如下這個樣子的:
{  
    resultCount = 1;  
    results =     (  
                {  
            artistId = 開發者 ID;  
            artistName = 開發者名稱; 
            price = 0; 
            isGameCenterEnabled = 0;  
            kind = software;  
            languageCodesISO2A =             (  
                EN  
            ); 
            trackCensoredName = 審查名稱;  
            trackContentRating = 評級;  
            trackId = 應用程序 ID;  
            trackName = 應用程序名稱";  
            trackViewUrl = 應用程序介紹網址;  
            userRatingCount = 用戶評級;  
            userRatingCountForCurrentVersion = 1;  
            version = 版本號;  
            wrapperType = software; 
      }  
    );  
}  

然後從中取得 results 數組即可,具體代碼如下所示:

NSDictionary *jsonData = [dataPayload JSONValue];  
NSArray *infoArray = [jsonData objectForKey:@"results"];  
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];  
NSString *latestVersion = [releaseInfo objectForKey:@"version"];  
NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];  

如果你拷貝 trackViewUrl 的實際地址,然後在瀏覽器中打開,就會打開你的應用程序在 appstore 中的介紹頁面。當然我們也可以在代碼中調用 safari 來打開它。
UIApplication *application = [UIApplication sharedApplication];  
[application openURL:[NSURL URLWithString:trackViewUrl]];  


  1. -(void)onCheckVersion:(NSString *)currentVersion  
  2. {  
  3.     NSString *URL = APP_URL;  
  4.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  
  5.     [request setURL:[NSURL URLWithString:URL]];  
  6.     [request setHTTPMethod:@"POST"];  
  7.     NSHTTPURLResponse *urlResponse = nil;  
  8.     NSError *error = nil;  
  9.     NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
  10.       
  11.     NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];  
  12.     NSDictionary *dic = [results JSONValue];  
  13.     NSArray *infoArray = [dic objectForKey:@"results"];  
  14.     if ([infoArray count]) {  
  15.         NSDictionary *releaseInfo = [infoArray objectAtIndex:0];  
  16.         NSString *lastVersion = [releaseInfo objectForKey:@"version"];  
  17.           
  18.         if (![lastVersion isEqualToString:currentVersion]) {  
  19.             trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];  
  20.             UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"關閉" otherButtonTitles:@"更新", nil] autorelease];  
  21.             [alert show];  
  22.         }  
  23.     }  
  24. }  

發佈了13 篇原創文章 · 獲贊 12 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章