如果我們需要實現(xiàn)版本的 app 自動更新跺涤,那么我們需要獲取當(dāng)前運(yùn)行程序的版本信息和 appstore 里發(fā)布的最新版本信息请垛。
當(dāng)前運(yùn)行程序的版本信息遏暴,可以在 mainBundle 里面獲劝嗤凇:
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
而 appstore 里發(fā)布的最新版本信息獲取稍微復(fù)雜一些枫绅,有兩種方案泉孩,思路都是一樣的:
其一:在某個服務(wù)器上存儲最新發(fā)布的版本信息,需要的時候向該服務(wù)器查詢并淋;
其二:在需要的時候向 appstore 查詢寓搬;
在這里我來介紹第二種方法:向 appstore 查詢應(yīng)用程序信息,包括作者县耽,版本句喷,app 介紹頁面地址等信息。
英文好的同學(xué)可以參考 apple 的文檔:www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
具體步驟如下:
1兔毙,用 POST 方式發(fā)送請求:
http://itunes.apple.com/search?term=你的應(yīng)用程序名稱&entity=software
更加精準(zhǔn)的做法是根據(jù) app 的 id 來查找:
http://itunes.apple.com/lookup?id=你的應(yīng)用程序的ID
如果是中國地區(qū)的程序唾琼,用這個:http://itunes.apple.com/cn/lookup?id=
2,從獲得的 response 數(shù)據(jù)中解析需要的數(shù)據(jù)澎剥。因為從 appstore 查詢得到的信息是 JSON 格式的锡溯,所以需要經(jīng)過解析。解析之后得到的原始數(shù)據(jù)就是如下這個樣子的:
{
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 nn* Open and Save your Citi Research in your favorite PDF Reader and Library such as iBooksnn* 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 數(shù)組即可哑姚,具體代碼如下所示:
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 的實際地址祭饭,然后在瀏覽器中打開,就會打開你的應(yīng)用程序在 appstore 中的介紹頁面蜻懦。當(dāng)然我們也可以在代碼中調(diào)用 safari 來打開它甜癞。
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString:trackViewUrl]];
這是評論的地址:
在iPhone應(yīng)用里直接打開app store 評論頁面的方法:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=abc"]];
復(fù)制代碼
更換下id號就可以。
如果想要打開下載頁面宛乃,把url改為
itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=40461254
更換下id號就可以悠咱。