有时候为了需求,我们需要检测app版本更新今天在这里整合下
//获取当前版本号
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
CFShow((__bridge CFTypeRef)(infoDictionary));
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
//获取苹果商店的版本号
NSError *error;
NSString * urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=你要检测的app版本"];
NSURL * url =[NSURL URLWithString:urlStr];
NSURLRequest * request =[NSURLRequest requestWithURL:url];
NSData * response =[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary * appInfo =[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"error:%@",[error description]);
}
NSArray *resultsArray =[appInfo objectForKey:@"results"];
if (![resultsArray count]) {
NSLog( @"error: nil");
return self;
}
NSDictionary * infoDic =[resultsArray objectAtIndex:0];
NSString * appVersion = [infoDic objectForKey:@"version"];
appUrl =[infoDic objectForKey:@"trackViewUrl"];
double doucurrV =[app_Version doubleValue];
double douappV= [appVersion doubleValue];
//判断版本号对比
if (doucurrV < douappV) {
NSString * titleStr =[NSString stringWithFormat:@"检查更新"];
NSString * message =[NSString stringWithFormat:@"发现新版本,是否更新?"];
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:titleStr message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.tag = 1001;
[alert show];
}else
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"检测更新" message:@"无更新版本"delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.tag = 1002;
[alert show];
}
//跳转更新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1001) {
if (buttonIndex == 1) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString: appUrl]];
}
}
}