iOS 检测版本更新

  这是我在项目中关于版本更新的做法,先说一下大体思路。

  思路:在程序启动时从服务器获取当前最新版本号,并与本地版本号进行比较。

  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]; } } } }]; }

 

转载于:https://www.cnblogs.com/pretty-guy/p/4548987.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值