如果我們要檢測app版本的更新,那么我們必須獲取當前運行app版本的版本信息和appstore 上發布的最新版本的信息。
當前運行版本信息可以通過info.plist文件中的bundle version中獲取:
?
- NSDictionary?*infoDic?=?[[NSBundle?mainBundle]?infoDictionary];??
- ];??
?
這樣就獲取到當前運行的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]]; ?
代碼如下:
?
-(void)onCheckVersion
{
? ??NSDictionary?*infoDic = [[NSBundle?mainBundle]?infoDictionary];
? ??//CFShow((__bridge CFTypeRef)(infoDic));
? ??NSString?*currentVersion = [infoDic?objectForKey:@"CFBundleVersion"];
? ??NSString?*URL =?@"http://itunes.apple.com/lookup?id=你的應用程序的ID";
? ??NSMutableURLRequest?*request = [[NSMutableURLRequest?alloc]?init];
? ? [request?setURL:[NSURL?URLWithString:URL]];
? ? [request?setHTTPMethod:@"POST"];
? ??NSHTTPURLResponse?*urlResponse =?nil;
? ??NSError?*error =?nil;
? ??NSData?*recervedData = [NSURLConnection?sendSynchronousRequest:request?returningResponse:&urlResponseerror:&error];
?? ?
? ??NSString?*results = [[NSString?alloc]?initWithBytes:[recervedData?bytes]?length:[recervedData?length]encoding:NSUTF8StringEncoding];
? ??NSDictionary?*dic = [results?JSONValue];
? ??NSArray?*infoArray = [dic?objectForKey:@"results"];
? ??if?([infoArray?count]) {
? ? ? ??NSDictionary?*releaseInfo = [infoArray?objectAtIndex:0];
? ? ? ??NSString?*lastVersion = [releaseInfo?objectForKey:@"version"];
?? ? ? ?
? ? ? ??if?(![lastVersion?isEqualToString:currentVersion]) {
? ? ? ? ? ??//trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];
? ? ? ? ? ??UIAlertView?*alert = [[UIAlertView?alloc]?initWithTitle:@"更新"?message:@"有新的版本更新,是否前往更新?"delegate:self?cancelButtonTitle:@"關閉"?otherButtonTitles:@"更新",?nil];
? ? ? ? ? ? alert.tag?=?10000;
? ? ? ? ? ? [alert?show];
? ? ? ? }
? ? ? ??else
? ? ? ? {
? ? ? ? ? ??UIAlertView?*alert = [[UIAlertView?alloc]?initWithTitle:@"更新"?message:@"此版本為最新版本"?delegate:selfcancelButtonTitle:@"確定"?otherButtonTitles:nil,?nil];
? ? ? ? ? ? alert.tag?=?10001;
? ? ? ? ? ? [alert?show];
? ? ? ? }
? ? }
}
- (void)alertView:(UIAlertView?*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
? ??if?(alertView.tag==10000) {
? ? ? ??if?(buttonIndex==1) {
? ? ? ? ? ??NSURL?*url = [NSURL?URLWithString:@"https://itunes.apple.com"];
? ? ? ? ? ? [[UIApplication?sharedApplication]openURL:url];
? ? ? ? }
? ? }
}