如果我們需要實現版本的 app 自動更新,那麼我們需要擷取當前運行程式的版本資訊和 appstore 裡發布的最新版本資訊。
當前運行程式的版本資訊,可以在 mainBundle 裡面擷取:
[cpp]
view plaincopyprint?
- NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
- NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
而 appstore 裡發布的最新版本資訊擷取稍微複雜一些,有兩種方案,思路都是一樣的:
其一:在某個伺服器上儲存最新發行的版本資訊,需要的時候向該伺服器查詢;
其二:在需要的時候向 appstore 查詢;
在這裡我來介紹第二種方法:向 appstore 查詢應用程式資訊,包括作者,版本,app 介紹頁面地址等資訊。
英文好的同學可以參考 apple 的文檔:www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
具體步驟如下:
1,用 POST 方式發送請求:
http://itunes.apple.com/search?term=你的應用程式名稱&entity=software
更加精準的做法是根據 app 的 id 來尋找:
http://itunes.apple.com/lookup?id=你的應用程式的ID
2,從獲得的 response 資料中解析需要的資料。因為從 appstore 查詢得到的資訊是 JSON 格式的,所以需要經過解析。解析之後得到的未經處理資料就是如下這個樣子的:
[html]
view plaincopyprint?
- {
- resultCount = 1;
- results = (
- {
- artistId = 301724683;
- artistName = Citibank;
- artistViewUrl = "http://itunes.apple.com/us/artist/citibank/id301724683?uo=4";
- artworkUrl100 = "http://a5.mzstatic.com/us/r1000/117/Purple/a1/85/a9/mzl.hvwnfdkw.png";
- artworkUrl512 = "http://a5.mzstatic.com/us/r1000/117/Purple/a1/85/a9/mzl.hvwnfdkw.png";
- artworkUrl60 = "http://a2.mzstatic.com/us/r1000/099/Purple/67/86/7e/mzi.utfdvrgy.png";
- averageUserRating = "3.5";
- averageUserRatingForCurrentVersion = 5;
- contentAdvisoryRating = "4+";
- currency = USD;
- description = "Description of you app.";
- features = (
- iosUniversal
- );
- fileSizeBytes = 4141195;
- genreIds = (
- 6015
- );
- genres = (
- Finance
- );
- ipadScreenshotUrls = (
- "http://a1.mzstatic.com/us/r1000/095/Purple/e0/a6/17/mzl.pbbxcjzt.1024x1024-65.jpg",
- "http://a3.mzstatic.com/us/r1000/036/Purple/cc/14/98/mzl.dyairego.1024x1024-65.jpg"
- );
- isGameCenterEnabled = 0;
- kind = software;
- languageCodesISO2A = (
- EN
- );
- price = 0;
- primaryGenreId = 6015;
- primaryGenreName = Finance;
- releaseDate = "2011-01-24T06:14:35Z";
- releaseNotes = "* View Real-time streaming prices for U.S. Treasuries \n\n* Open and Save your Citi Research in your favorite PDF Reader and Library such as iBooks\n\n* Search for your favorite videos";
- screenshotUrls = (
- "http://a3.mzstatic.com/us/r1000/066/Purple/17/51/fb/mzl.zywiavgn.png",
- "http://a5.mzstatic.com/us/r1000/026/Purple/73/85/97/mzl.csmdtndk.png"
- );
- sellerName = "Citibank, N.A.";
- sellerUrl = "http://";
- supportedDevices = (
- all
- );
- trackCensoredName = "Citi Velocity";
- trackContentRating = "4+";
- trackId = 414697122;
- trackName = "Citi Velocity";
- trackViewUrl = "http://itunes.apple.com/us/app/citi-velocity/id414697122?mt=8&uo=4";
- userRatingCount = 5;
- userRatingCountForCurrentVersion = 1;
- version = "1.4";
- wrapperType = software;
- }
- );
- }
然後從中取得 results 數組即可,具體代碼如下所示:
[cpp]
view plaincopyprint?
- 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 來開啟它。
[cpp]
view plaincopyprint?
- UIApplication *application = [UIApplication sharedApplication];
- [application openURL:[NSURL URLWithString:trackViewUrl]];
轉載:http://blog.csdn.net/kesalin/article/details/6605934