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

s实现:

强制更新:每次弹框

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

代码如下:

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

步骤一: 将检测更新写到APPDelegate中
- (void)applicationDidBecomeActive:(UIApplication *)application
{ [JFUpdateVersion upData]; }
步骤二: 完善检测更新方法
JFUpdateVersion.m

#import "JFUpdateVersion.h" #import "AppDelegate.h" @implementation JFUpdateVersion + (void)upData
{ //当前手机的版本号 NSString *ver = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"]; BaseRequest *request = [BaseRequest sharedBaseRequest]; [request post:G.YXM_act_version otherPar:G.YX_act_version params:@{} success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"status"]integerValue] == 0) { NSLog(@"%@",responseObject); [[[JFUpdateVersion alloc]init]compareVersionLocalVerson:ver appVerson:responseObject[@"data"][@"version_ios"] andtype:[responseObject[@"data"][@"type"] integerValue] andURl:responseObject[@"data"][@"url"]]; [UserDefaults setObject:responseObject[@"data"][@"url_ios"] forKey:@"AppURL"]; }else{ } } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"error:%@",error); }]; }

//步骤三:比较版本号
- (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{ if ([IsHadShowUpdate isEqualToString:@"1"]) {//如果已经展示过提示框则不展示了 return; } 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]) { //假如不是同一天,更新存储的日期,并且把flag置为YES [UserDefaults setObject:nowday forKey:@"versionUpdateKey"]; [UserDefaults setObject:@"0" forKey:@"IsHadShowUpdate"]; [self canChooseUpdate]; }else{//如果是同一天的话 [UserDefaults setObject:@"1" forKey:@"IsHadShowUpdate"]; return; } } }else{ } }
//步骤四:强制更新
- (void)showForceUpdate{
    //弹出提示更新弹框
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"亲,有新版本了" message:@"为了给您更好的体验,请点击立即更新!" preferredStyle:UIAlertControllerStyleAlert];
    
    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:action1];
    UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
    [vc presentViewController:alertVc animated:YES completion:nil];
}
//步骤五:可选更新
-(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];
}

转载请写明出处。

猜你喜欢

转载自www.cnblogs.com/lishanshan/p/10209001.html