【源码】iOS-App版本更新提示
//检查版本更新
-(void)VersionRequestApi
{
//异步线程//处理耗时操作的代码块
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//App内info.plist文件里面版本号
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
CurrentVersion = infoDict[@"CFBundleShortVersionString"];//本地的版本号
// 取得AppStore信息
MyURL = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@", @"1336413759"];
[[ApiTool shareApiTool] post:MyURL params:nil success:^(NSDictionary *jsonDic) {
NSArray *results = [jsonDic objectForKey:@"results"];
AppStoreVersion = [results[0] objectForKey:@"version"];//appstore的版本号
ReleaseNotes = [results[0] objectForKey:@"releaseNotes"];//更新描述
trackViewUrl = [results[0] objectForKey:@"trackViewUrl"];//跳转AppStore需要的url
NSLog(@"AppStoreVersion:%@,ReleaseNotes:%@,trackViewUrl:%@",AppStoreVersion,ReleaseNotes,trackViewUrl);
//比较版本号
NSArray *curVerArr = [CurrentVersion componentsSeparatedByString:@"."];
NSArray *appstoreVerArr = [AppStoreVersion componentsSeparatedByString:@"."];
BOOL needUpdate = NO;//是否需要更新
//比较版本号大小
int maxv = (int)MAX (curVerArr.count, appstoreVerArr.count);
int cver = 0;
int aver = 0;
for (int i = 0; i < maxv; i++)
{
if (appstoreVerArr.count > i)
{
aver = [NSString stringWithFormat:@"%@", appstoreVerArr[i]].intValue;
}
else
{
aver = 0;
}
if (curVerArr.count > i)
{
cver = [NSString stringWithFormat:@"%@", curVerArr[i]].intValue;
}
else
{
cver = 0;
}
if (aver == cver)
{
continue;
}
else if (aver > cver)
{
needUpdate = YES;
break;
}
else
{
needUpdate = NO;
break;
}
}
if (needUpdate) {
//弹提示框
NSLog(@"弹提示框");
SVAlertView *alertView = [[SVAlertView alloc] init];
[alertView
showAlertViewWithMessage:ReleaseNotes
andTitle:@"升级提示"
confirmTitle:@"马上更新"
cancelTitle:@"以后再说"
ConfirmAction:^{
NSLog(@"跳到appstore");
//跳到appstore
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];
}
andCancelAction:^{
NSLog(@"点击了取消");
}];
}
} failure:^(NSError *error) {
[MyHUD showInView:ShareApp.window text:@"版本号获取失败"];
NSLog(@"error:%@",error);
}];
});
}