app在程序中设置版本自动更新的步骤
1.通过post方式发送请求:http://itunes.apple.com/lookup?id=你的应用程序的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;
}
);
}
#pragma direct open itunes 此部分代码为实现在app直接调用手机的app store应用并导向本app的链
接页面
-(void)openReferralURL:(NSURL *)referralURL{
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:[NSURLRequest
requestWithURL:referralURL] delegate:self startImmediately:YES];
[con release];
}
-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)
request redirectResponse:(NSURLResponse *)response{
self.iTunesURL = [response URL];
if ([self.iTunesURL.host hasSuffix:@"itunes.apple.com"]) {
[connection cancel];
[self connectionDidFinishLoading:connection];
return nil;
}else{
return request;
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[[UIApplication sharedApplication] openURL:self.iTunesURL];
}
#end pragram
-(void)afterItuensConnect:(ASIHTTPRequest *)request//请求完成之后调用的函数
{
NSString *responseStr = [request responseString];//NSLog(@"%@",responseStr);
NSDictionary *dic = [responseStr JSONValue];
NSArray *infoArrays = [dic objectForKey:@"results"];
NSDictionary *releaseInfo = [infoArrays objectAtIndex:0];
self.itunes_url = [releaseInfo objectForKey:@"trackViewUrl"];
NSString *version =[releaseInfo objectForKey:@"version"];//最新版本号,itunes上的为最新
的,而不是程序中的
NSString *lastVersion =[[NSBundle mainBundle]
objectForInfoDictionaryKey:@"CFBundleVersion"];//本次版本的版本号,在info.plist文件中设置的
Bundle Version字段
NSString *l_Path = [[lastVersion componentsSeparatedByString:@"."] objectAtIndex:0];
NSString *p_Path = [[version componentsSeparatedByString:@"."] objectAtIndex:0];
BOOL result = [l_Path compare:p_Path] == NSOrderedAscending;
BOOL error_result = [p_Path compare:l_Path] == NSOrderedAscending;
if (error_result) {
return;
}
if ([lastVersion isEqualToString:version]) {
//版本号相同,无任何操作
}
else if (result) {//第一个版本号大,强制更新
update_tab = @"closeApp";
UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"" message:
[self.appIdAndAlertStr objectForKey:@"MandatoryUpdateStr"] delegate:self
cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alerView show];
[alerView release];
}else{//非强制性更新
update_tab = @"";
UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"" message:
[self.appIdAndAlertStr objectForKey:@"SelectiveUpdateStr"] delegate:self
cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alerView show];
[alerView release];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if ([alert_lab isEqualToString:@"no_network"]) {
exit(0);
}
if(buttonIndex>0){//click "yes" in update app alert
// turn to update app web page..
NSURL *url = [NSURL URLWithString: self.itunes_url];
[self openReferralURL:url];
}else{//click "no"
if ([update_tab isEqualToString:@"closeApp"]) {
exit(0);
}
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;
}
);
}
#pragma direct open itunes 此部分代码为实现在app直接调用手机的app store应用并导向本app的链
接页面
-(void)openReferralURL:(NSURL *)referralURL{
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:[NSURLRequest
requestWithURL:referralURL] delegate:self startImmediately:YES];
[con release];
}
-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)
request redirectResponse:(NSURLResponse *)response{
self.iTunesURL = [response URL];
if ([self.iTunesURL.host hasSuffix:@"itunes.apple.com"]) {
[connection cancel];
[self connectionDidFinishLoading:connection];
return nil;
}else{
return request;
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[[UIApplication sharedApplication] openURL:self.iTunesURL];
}
#end pragram
-(void)afterItuensConnect:(ASIHTTPRequest *)request//请求完成之后调用的函数
{
NSString *responseStr = [request responseString];//NSLog(@"%@",responseStr);
NSDictionary *dic = [responseStr JSONValue];
NSArray *infoArrays = [dic objectForKey:@"results"];
NSDictionary *releaseInfo = [infoArrays objectAtIndex:0];
self.itunes_url = [releaseInfo objectForKey:@"trackViewUrl"];
NSString *version =[releaseInfo objectForKey:@"version"];//最新版本号,itunes上的为最新
的,而不是程序中的
NSString *lastVersion =[[NSBundle mainBundle]
objectForInfoDictionaryKey:@"CFBundleVersion"];//本次版本的版本号,在info.plist文件中设置的
Bundle Version字段
NSString *l_Path = [[lastVersion componentsSeparatedByString:@"."] objectAtIndex:0];
NSString *p_Path = [[version componentsSeparatedByString:@"."] objectAtIndex:0];
BOOL result = [l_Path compare:p_Path] == NSOrderedAscending;
BOOL error_result = [p_Path compare:l_Path] == NSOrderedAscending;
if (error_result) {
return;
}
if ([lastVersion isEqualToString:version]) {
//版本号相同,无任何操作
}
else if (result) {//第一个版本号大,强制更新
update_tab = @"closeApp";
UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"" message:
[self.appIdAndAlertStr objectForKey:@"MandatoryUpdateStr"] delegate:self
cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alerView show];
[alerView release];
}else{//非强制性更新
update_tab = @"";
UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"" message:
[self.appIdAndAlertStr objectForKey:@"SelectiveUpdateStr"] delegate:self
cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alerView show];
[alerView release];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if ([alert_lab isEqualToString:@"no_network"]) {
exit(0);
}
if(buttonIndex>0){//click "yes" in update app alert
// turn to update app web page..
NSURL *url = [NSURL URLWithString: self.itunes_url];
[self openReferralURL:url];
}else{//click "no"
if ([update_tab isEqualToString:@"closeApp"]) {
exit(0);
}