iOS-app更新和强制更新

版本号

规则

版本号的格式:v<主版本号>.<副版本号>.<发布号>
如版本号为2.3.6
1. 我一般把第一位作为大版本号。如出现重大更新,如果用户不更新,这个app都用不下去了。这个时候就要强制用户更新。
2. 第二位作为功能版本号。比如增加了一些新的功能。这个时候通过增加这个版本号,来添加功能。
3. 第三位作为修订版本号。如,上线后出现了一个bug,这个bug需要及时修复,这个时候就增加这个修订版本号。

更新思路

强制更新:

在出现第一位版本号变化的时候,强制更新。通过appStore上的版本和当前的版本对比。

可选更新:

在非第一位版本号变化的时候,用户可以选择暂时忽略(下次还会出现这个弹窗),跳过(下次打开不出现),或者更新app。

具体看后面代码⤵️

效果

  1. 强制更新
    这里写图片描述
  2. 可选更新
    这里写图片描述

部分代码段

1.获取appstore上的内容

- (void)checkVersion{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[LGAppInfo appUrlInItunes] parameters:nil progress:nil success:^(NSURLSessionDataTask *task,id responseObject) {
        LGLog(@"%@",responseObject);
        //1.是否请求成功
        if (((NSArray *)responseObject[@"results"]).count<=0) return;
        //2.获取appstore版本号和提示信息
        self.storeVersion = [(NSArray *)responseObject[@"results"] firstObject][@"version"];
          NSString *releaseNotes = [(NSArray *)responseObject[@"results"] firstObject][@"releaseNotes"];
        //3.获取跳过的版本号
        NSString *skipVersion = [[NSUserDefaults standardUserDefaults] valueForKey:skipVersionKey];
        //4.比较版本号
        LGLog(@"%@--%@",self.storeVersion,skipVersion);
        if ([self.storeVersion isEqualToString:skipVersion]) {//如果store和跳过的版本相同
            return;
        }else{
            [self compareCurrentVersion:[LGAppInfo appVersion] withAppStoreVersion:self.storeVersion updateMsg:releaseNotes];
        }
    } failure:nil];
}

2.比较版本

/**
 当前版本号和appstore比较

 @param currentVersion 当前版本
 @param appStoreVersion appstore上的版本
 @param updateMsg 更新内容
 */
- (void)compareCurrentVersion:(NSString *)currentVersion withAppStoreVersion:(NSString *)appStoreVersion updateMsg:(NSString *)updateMsg{
    NSMutableArray *currentVersionArray = [[currentVersion componentsSeparatedByString:@"."] mutableCopy];
    NSMutableArray *appStoreVersionArray = [[appStoreVersion componentsSeparatedByString:@"."] mutableCopy];
    if (!currentVersionArray.count ||!appStoreVersionArray.count) return;
    //修订版本号
    int modifyCount = abs((int)(currentVersionArray.count - appStoreVersionArray.count));
    if (currentVersionArray.count > appStoreVersionArray.count) {
        for (int index = 0; index < modifyCount; index ++) {
            [appStoreVersionArray addObject:@"0"];
        }
    } else if (currentVersionArray.count < appStoreVersionArray.count) {
        for (int index = 0; index < modifyCount; index ++) {
            [currentVersionArray addObject:@"0"];
        }
    }
    //大版本必须强制更新<及 第一位表示大版本>
    if ([currentVersionArray.firstObject integerValue] > [appStoreVersionArray.firstObject integerValue]) {
        //强制更新---
        [self showUpdateAlertMust:YES withStoreVersion:appStoreVersion message:updateMsg];
    }else{//不需要强制更新 检查后面的版本号,如果比appstore大  则更新
        for (int index = 0; index<currentVersionArray.count; index++) {
            if ([currentVersionArray[index] integerValue]> [appStoreVersionArray[index] integerValue]) {
                [self showUpdateAlertMust:NO withStoreVersion:appStoreVersion message:updateMsg];
                return;
            }
        }
    }
}

3.跳转界面

/**
 打开appstore 执行更新操作
 */
- (void)openAppStoreToUpdate{
    LGLog(@"打开到appstore");
    NSURL *trackUrl = [NSURL URLWithString:self.trackViewUrl];
    if ([LGApplication canOpenURL:trackUrl]) {
        [[UIApplication sharedApplication] openURL:trackUrl];
    }
}

使用方法

在appdelegate.m中,程序唤醒后调用如下:

- (void)applicationDidBecomeActive:(UIApplication *)application {

    [[LGCheckVersion shareCheckVersion] checkVersion];

}

demo地址

https://github.com/LGLee/LGCheckUpdate.git

点个赞送大胸美女一枚

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值