- (BOOL)checkHaveNewVersion_Func
{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
__block BOOL bHaveNewVersionFlag = FALSE; // 有新版本标志
// // 首先取得本地程序版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
//这个地方 一定要注意,因为appstore里返回的版本信息最前面加了个V 所以这个地方必须也加个
NSString *localVersion = [NSString stringWithFormat:@"V%@",[infoDic objectForKey:@"CFBundleShortVersionString"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 发送检测版本数据到app store
NSString *URL = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", APPID];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse *urlResponse = nil;
__block NSError *error = nil;
NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中: json4种方式用法及比较http://blog.csdn.net/enuola/article/details/7903632
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableLeaves error:&error];
// 解析数据
NSArray *infoArray = [dic objectForKey:@"results"];
if (0 < [infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *appStoreVersion = [releaseInfo objectForKey:@"version"];
// appstore版本比本地版本高,则可更新
if (NSOrderedAscending == [localVersion compare:appStoreVersion]) { // 有新版本
bHaveNewVersionFlag = YES;
[MBProgressHUD hideHUDForView:self.view animated:YES];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:MY_Local_Str(@"温馨提示") message:MY_Local_Str(@"已有新版本,是否前去更新") delegate:self cancelButtonTitle:MY_Local_Str(@"暂不更新") otherButtonTitles:MY_Local_Str(@"更新"), nil];
[alertView show];
} else { // 无新版本
bHaveNewVersionFlag = NO;
[MBProgressHUD hideHUDForView:self.view animated:YES];
[GlobalClass promptWithString:@"当前程序已是最新版本" onView:self.view];
}
} else {
bHaveNewVersionFlag = NO;
[MBProgressHUD hideHUDForView:self.view animated:YES];
[GlobalClass promptWithString:@"当前程序已是最新版本" onView:self.view];
}
});
});
return bHaveNewVersionFlag;
}