中心点:比较线上的版本号的每一位 与现在程序中的每一位是否相同 从后到前不同位置更新的程度不同。提示的语言也不同。中间的参数kStoreAppId 是APPID。
#pragma mark 检查版本更新
+(void)checkAppUpdate
{
//线程
dispatch_queue_t appleQueue = dispatch_queue_create("apple", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(appleQueue, ^{
//进入appleQueue的子线程了,在这儿爱干嘛干嘛,记得干完了回主线程
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *nowVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kStoreAppId]];
NSString * file = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
if (file==nil||file.length==0) {
return;
}
NSDictionary *dicFile=[self dictionaryWithJsonString:file];
if (dicFile==nil) {
return;
}
NSArray *arrResult=[dicFile objectForKey:@"results"];
if (arrResult==nil||arrResult.count==0) {
return;
}
NSDictionary *dicResult=[arrResult objectAtIndex:0];
if (dicResult==nil) {
return;
}
if (![[dicResult allKeys]containsObject:@"version"]) {
return;
}
///appStore版本
NSString *newVersion =[dicResult objectForKey:@"version"];
NSInteger lastVersionF = [[[newVersion componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
NSInteger lastVersionS = [[[newVersion componentsSeparatedByString:@"."] objectAtIndex:1] integerValue];
NSInteger lastVersionT = [[[newVersion componentsSeparatedByString:@"."] objectAtIndex:2] integerValue];
///本地版本
NSInteger nowVersionF=[[[nowVersion componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
NSInteger nowVersionS=[[[nowVersion componentsSeparatedByString:@"."] objectAtIndex:1] integerValue];
NSInteger nowVersionT=[[[nowVersion componentsSeparatedByString:@"."] objectAtIndex:2] integerValue];
NSString *str_Title=@"";
NSString *newDes=@"";
if (nowVersionF < lastVersionF) {
str_Title=@"软件全面更新提示";
newDes=[NSString stringWithFormat:@"系统全面升级\n请重新下载新版本应用!\n 更新内容:\n %@",[dicResult objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str_Title message:newDes delegate:self cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
if (nowVersionF <= lastVersionF && nowVersionS < lastVersionS) {
str_Title=@"软件重要更新提示";
newDes=[NSString stringWithFormat:@"有新的版本更新,是否前往更新?\n更新内容:\n %@",[dicResult objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str_Title message:newDes delegate:self cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
if (nowVersionF <= lastVersionF && nowVersionS <= lastVersionS && nowVersionT < lastVersionT) {
str_Title=@"软件优化更新提示";
newDes=[NSString stringWithFormat:@"有新的版本更新,是否前往更新?\n 更新内容:\n %@",[dicResult objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str_Title message:newDes delegate:self cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
//完事儿了,回主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}