前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ios 版本更新提示-硬更新/软更新

ios 版本更新提示-硬更新/软更新

作者头像
用户1219438
发布2019-01-03 11:02:14
1.2K0
发布2019-01-03 11:02:14
举报
文章被收录于专栏:AliceAlice

实现:

强制更新:每次弹框

非强制更新:一天提示一次

代码如下:

步骤一: 将检测更新写到APPDelegate的applicationDidBecomeActive中

步骤二: 检测是否需要更新

步骤三: 针对非强制更新-首先判断日期如果是同一天的话就不提示更新,如果不是同一天可以提示更新

逻辑如下:前边是之前的逻辑 简单解释一下:0代表未提示更新   1代表已经提示更新  存日期表示将日期存到内存中

前边是第一次的逻辑,后边是写代码时候的逻辑,更简化了一些

下边附上核心代码:

代码语言:javascript
复制
- (void)compareVersionLocalVerson:(NSString *)localVerson appVerson:(NSString *)appVerson andtype:(NSInteger)type andURl:(NSString *)url{
    
    //将版本号按照.切割后存入数组中
    NSArray *localArray = [localVerson componentsSeparatedByString:@"."];
    NSArray *appArray = [appVerson componentsSeparatedByString:@"."];
    NSInteger minArrayLength = MIN(localArray.count, appArray.count);
    BOOL needUpdate = NO;
    
    for(int i=0;i<minArrayLength;i++){//以最短的数组长度为遍历次数,防止数组越界
        
        //取出每个部分的字符串值,比较数值大小
        NSString *localElement = localArray[i];
        NSString *appElement = appArray[i];
        
        NSInteger  localValue =  localElement.integerValue;
        
        NSInteger  appValue = appElement.integerValue;
        
        if(localValue<appValue) {
            //从前往后比较数字大小,一旦分出大小,跳出循环
            needUpdate = YES;
            break;
        }else if(localValue>appValue){
            needUpdate = NO;
            break;
        }
        
    }
    if (needUpdate) {
        if (type == 1) {//强制更新
            
            [self showForceUpdate];
            
        }else{
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            NSString *nowday = [formatter stringFromDate:[NSDate date]];
            NSString *saveDay = [UserDefaults objectForKey:@"versionUpdateKey"];
            if (saveDay == nil) {
                saveDay = @"";
            }
            
            if (![saveDay isEqualToString:nowday]) { //假如不是同一天,更新存储的日期,并且把isHadShowUpdate 设置成yes

                [self canChooseUpdate];
                
                [UserDefaults setObject:@"1" forKey:@"isHadShowUpdate"];
                [UserDefaults setObject:nowday forKey:@"versionUpdateKey"];
                
            }else{//如果是同一天的话
                return;
//                if([IsHadShowUpdate  isEqualToString:@"0"]){
//                    [self canChooseUpdate];
//                    [UserDefaults setObject:@"1" forKey:@"isHadShowUpdate"];
//                    [UserDefaults setObject:nowday forKey:@"versionUpdateKey"];
//                }else{
//                   return;
//                }
  
            }
        }
        
        
    }else{
        
    }
}

非强制更新代码

代码语言:javascript
复制
//可选更新
-(void)canChooseUpdate{
    //弹出提示更新弹框
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"亲,有新版本了" message:@"更稳定、快速、多彩的功能和体验,点击立即更新!" preferredStyle:UIAlertControllerStyleAlert];
    //
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSString *JumpURL = [[NSUserDefaults standardUserDefaults]objectForKey:@"AppURL"];
        
        if(JumpURL.length ==0){
            [JKToast showWithText:@"参数错误"];
            return;
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:JumpURL]];
            AppDelegate *app = appDelegate;
            UIWindow *window = app.window;
            
            [UIView animateWithDuration:1.0f animations:^{
                window.alpha = 0;
                window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
            } completion:^(BOOL finished) {
                exit(0);
            }];
        }
        
    }];
    
    
    
    [alertVc addAction:action2];
    [alertVc addAction:action1];
    
    
    
    UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
    [vc presentViewController:alertVc animated:YES completion:nil];
}

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Menlo; color: #000000; background-color: #ffffff }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档