iOS 檢測版本更新
如果我們要檢測app版本的更新,那么我們必須獲取當前運行app版本的版本信息和appstore 上發布的最新版本的信息。
當前運行版本信息可以通過info.plist文件中的 version中獲取:
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
這樣就獲取到當前運行的app的版本了
具體步驟如下:
1,用 POST 方式發送請求:
http://itunes.apple.com/lookup?id=你的應用程序的ID
#define APP_URL http://itunes.apple.com/lookup?id=你的應用程序的ID
你的應用程序的ID 是 itunes connect里的 Apple ID
NSString *URL = @"http://itunes.apple.com/lookup";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
// ? 數據體
NSString *str = [NSString stringWithFormat:@"id=你的應用程序的ID"];
// 將字符串轉換成數據
request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
// 開始工作,在很多多線程技術中,start run
dispatch_async(dispatch_queue_create("demo", DISPATCH_QUEUE_CONCURRENT), ^{
[connection start];
});
NSURLConnection 代理中獲取數據處理數據-這里不多說了。。
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];
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:requestreturningResponse:&urlResponse error:&error];
NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedDatalength] encoding:NSUTF8StringEncoding];
NSDictionary *dic = [results JSONValue];
NSArray *infoArray = [dic objectForKey:@"results"];
if ([infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:];
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:self cancelButtonTitle:@"確定" 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:@"軟件在appstore中的鏈接"];
[[UIApplication sharedApplication]openURL:url];
}
}