iOS的版本号, 一个叫做Version, 一个叫做Build.
获得Version ,
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
获得build号:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
// 以下方法
先获取最新的版本号
再获取当前软件的版本号
最后进行比较
- (void)checkVersionUpdate{
//同步请求 JSON方法获取数据
NSError *error;
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kAppId];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *resquest = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:resquest returningResponse:nil error:nil];
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error) {
DLog(@"error: = %@", [error description]);
return ;
}
NSArray *resultArray = [appInfoDic objectForKey:@"results"];
if (![resultArray count]) {
DLog(@"error: resultArray == nil");
return;
}
NSDictionary *infoDic = [resultArray objectAtIndex:0];
NSString *lastVersion = [infoDic objectForKey:@"version"];
NSString *trackViewUrl = [infoDic objectForKey:@"trackViewUrl"];
NSString *trackName = [infoDic objectForKey:@"trackName"];
//获取此应用的版本号
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@" CFBundleShortVersionString"];
double doubleCurrentVersion = [currentVersion doubleValue];
double doubleUpdateVersion = [lastVersion doubleValue];
//比较
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];
if (doubleCurrentVersion < doubleUpdateVersion) {
NSString *messagestr = [NSString stringWithFormat:@"发现新版本:%@, 是否升级?", lastVersion];
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:titleStr message:messagestr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
alter.tag = [kAppId intValue];
[alter show];
}else{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:titleStr message:@"暂无新版本"delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
alter.tag = [kAppId intValue]+1;
[alter show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == [kAppId intValue]) {
if (buttonIndex == 1) {
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:_trackViewUrl]];
}
}
}