随诊医生2---检查版本更新

系统启动时,在AppDelegate.application方法中,初始化完友盟、SharedSDK和环信之后,就开始检查版本更新了。这时检查版本更新实际了有些早,因为如果需要进行版本灰度发布时,需要知道用户的ID,在此处检查版本更新则不能实现基于用户的灰度版本发布。

系统先初始化HTTP请求需要的类对象:

    _dtHttpHelper = [[DTHttpHelper alloc]init];
    _dtHttpHelper.delegate = self;
其中将HTTP访问类的回调代理设置为本类,就是当HTTP请求成功,有合法数据返回时,会调用本类定义的回调函数来进行处理。

AppDelegate.application中检查版本更新的代码如下所示:

- (void)checkVersionByServer{  
    NSString *urlStr = [NSString stringWithFormat:@"%@getVersion&roleId=2&platform=%@",ServerIPCMD,@"IOS"];     
    [_dtHttpHelper getDataFormServerWithUrlString:urlStr andCmdType:DTgetVersion];
}
当HTTP请求成功后,将调用如下的回调函数进行数据处理:

- (void)DTHttpHelperSuccessWithDataSource:(NSDictionary *)dataSource andCmdType:(NSInteger)cmdType{    
    if (cmdType == DTgetVersion)
    {
        if ([dataSource objectForKey:KStatus]!= [NSNull null] && [[dataSource objectForKey:KStatus] isEqualToString:KOk])
        {
            if([[dataSource objectForKey:KStatus] isEqualToString:KOk])
            {                
                if ([dataSource objectForKey:@"version"])
                {
                    NSString *newVersion = [dataSource objectForKey:@"version"];
                    NSString *curVersion = [[NSUserDefaults standardUserDefaults]objectForKey:KCurVersion];  
                    long long newVersionNum = [[NSString stringWithFormat:@"%@",[dataSource objectForKey:@"flagNum"]]longLongValue];
                    long long curVersionNum =  [[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:KCurVersionNum]]longLongValue];
                    if (newVersionNum > curVersionNum)
                    {
                        _downloadUrl = [NSString stringWithFormat:@"%@",[dataSource objectForKey:@"downloadUrl"]];
                        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:KHaveNewVersion];
                        [[NSUserDefaults standardUserDefaults]setObject:newVersion forKey:KNewVersion];
                        [[NSUserDefaults standardUserDefaults]synchronize];                        
                        NSString *isForced = [NSString stringWithFormat:@"%@",[dataSource objectForKey:@"isForced"]];                        
                        UIAlertView *versionAlert = nil;                        
                        if ([isForced isEqualToString:@"1"])
                        {
                            versionAlert = [[UIAlertView alloc]initWithTitle:@"请更新最新版本,否则无法使用。" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        }
                        else
                        {
                            versionAlert = [[UIAlertView alloc]initWithTitle:@"发现新版本,是否更新?" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
                        }                        
                        versionAlert.tag = 10001;                        
                        [versionAlert show];
                    }
                }
            }
        }
    }
}

版本号采用一个整数来表示,数值越大代表版本越新,分为强制更新和普通更新,强制更新如果用户不更新则不允许用户继续使用。

如果是强制更新时,消息框中只有一个确定按钮,而如果不是强制更新时,消息框中有确定和取消两个按钮,如果用户点取消按钮时,则直接向下执行,不进行版本更新操作。如果用户点击确定按钮,会调用AppDelegate中的回调函数:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{    
    if (alertView.tag == 10001)
    {
        if (buttonIndex == 0)
        {
            BOOL downloadApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_downloadUrl]];
            [self exitApplication];
        }
    }
如果用户点击的是版本提示的消息框,并且按的是确定按钮,则执行版本更新操作。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值