- 根据AppStore上的数据进行判断是否更新
苹果给了我们一个接口,可以根据应用的id请求应用的一些信息,取出其中的版本与当前运行的应用的版本号比较。
//同步请求获取应用的信息字典
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];
[request setHTTPMethod:@"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *releaseInfo = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSArray *resultArr = releaseInfo[@"results"];
NSDictionary *resultDict = resultArr[0];
//获取需要的version,trackViewUrl(更新应用的地址),trackName
NSString *latestVersion = [resultDict objectForKey:@"version"];
NSString *trackViewUrl1 = [resultDict objectForKey:@"trackViewUrl"];//地址trackViewUrl
NSString *trackName = [resultDict objectForKey:@"trackName"];//trackName
//获取应用当前的版本号
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];
double doubleCurrentVersion = [currentVersion doubleValue];
double doubleUpdateVersion = [latestVersion doubleValue];
//根据版本号比较判断是否更新
if (doubleCurrentVersion < doubleUpdateVersion) {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:trackName message:@"有新版本,是否升级!" delegate: self cancelButtonTitle:@"取消" otherButtonTitles: @"升级", nil];
alert.tag = 1001;
[alert show];
}
else{
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:trackName
message:@"暂无新版本"
delegate: nil
cancelButtonTitle:@"好的"
otherButtonTitles: nil, nil];
[alert show];
}
//如果需要更新,就跳转到下载页面,trackViewUrl是全路径,直接请求
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];
根据后台返回的数据进行判断是否更新
这种方法可以用在每次启动应用程序的时候做一次判断是否更新,可以根据后台返回的数据里取出版本号,与运行的应用当前的版本号做对比,或者当版本更新的时候后台返回一个bool类型的字段,判断下是否需要更新。
注意:当ios做更新版本的时候,如果有更新版本的按钮,如果只是显示版本号而没有点击响应事件的时候最好按钮置灰,否则审核的时候可能会审核不通过,若可以点击响应,则该更新按钮要和当前页面的其他按钮界面保持一致。