标签:
这是我在项目中关于版本更新的做法,先说一下大体思路。
思路:在程序启动时从服务器获取当前最新版本号,并与本地版本号进行比较。
1.若需要强制更新则只提供更新选项。
2.若是常规更新则提供“是”、“否”,若用户点击否,则在本地记录获取的版本号,若没有新的版本迭代,以后都不在提示更新
代码如下:
+ (void)checkVersionWithBlock:(void (^)(NSString *))theBlock
{ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:CheckVersionURLString]]; [self postDataWithPath:request withUseActivityIndicator:NO withResponseBlock:^(id result, NSError *err) { //1.获取当前版本号 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; if (result) { NSString *newVersion = result[@"version"]; BOOL isNeedForceUpdate = NO; BOOL isNeedUpdate = NO;
//判断需要强制更新、普通更新
//你的判断代码 if (isNeedForceUpdate) {//强制更新 [[WKAlertView shareInstance] showTitle:@"更新提示" withMessage:messageStr withCancelButton:nil withOtherButton:@"更新" withMessageAlignmentLeft:YES withHanderBlock:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 0) {//是 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UpdateVersionURLString]]; } }]; [[NSUserDefaults standardUserDefaults] setObject:@(isNeedForceUpdate) forKey:@"isNeedForceUpdate"]; }else if (isNeedUpdate){//提示更新 //上次检测更新的版本号 //此次检测更新的版本号 //是否取消过 NSString *lastChekedVersion = [userDefaults stringForKey:LastCheckedVersionKey]; BOOL isNeedShowAlert; if(![newVersion isEqualToString:lastChekedVersion]){//需要显示 isNeedShowAlert = YES; }else{ isNeedShowAlert = NO; } if (isNeedShowAlert) { [[WKAlertView shareInstance] showTitle:@"更新提示" withMessage:messageStr withCancelButton:@"否" withOtherButton:@"是" withMessageAlignmentLeft:YES withHanderBlock:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 1) {//是 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UpdateVersionURLString]]; } if (buttonIndex == 0) {//否 //存储上一次检测更新的版本串 [userDefaults setValue:newVersion forKey:LastCheckedVersionKey]; } }]; [[NSUserDefaults standardUserDefaults] setObject:@(isNeedUpdate) forKey:@"isNeedUpdate"]; [[NSUserDefaults standardUserDefaults]synchronize]; } } } }]; }
标签:
原文地址:http://www.cnblogs.com/pretty-guy/p/4548987.html