iOS 检测版本更新

2,742 阅读4分钟

方案一:自己维护版本升级

和后台同事定义并维护好一个版本升级接口,每次发布新版本后再在管理平台更新一下最新版本信息,移动端通过请求接口获取最新版本信息来提示用户是否需要升级,用户点击同意升级时直接跳转苹果商店就行。

这种方式方便控制是否强制升级等自定义操作。还可以在接口中返回自定义提示信息,比如此次更新的内容有哪些、需要强制升级的原因,都可以显示在版本升级弹窗中。

如果用户选择暂不升级,可以本地缓存一个数据,用于记录最近一次弹出版本升级弹窗的时间,并约定一个弹窗间隔时间,间隔时间内不再弹出弹窗(强制升级除外)。

.

方案二:检测苹果商店版本

有下面几个请求链接: (链接中的id:登录AppStore Connect -> App信息 -> Apple ID;从苹果商店分享出来的链接末尾也是这个id。还有bundleId:填你的App对应的bundleId就行了)。

1、(国外) https://itunes.apple.com/lookup?id=XXX
2、(国内) https://itunes.apple.com/cn/lookup?id=XXX
3、(国外) https://itunes.apple.com/lookup?bundleId=com.XXX
4、(国内) https://itunes.apple.com/cn/lookup?bundleId=com.XXX

试了下上面这几个链接,使用AFHTTPSessionManager发送POST请求,结果如下:

1、成功,但results目录下的数据为空
2、成功并返回所有信息
3、成功,但results目录下的数据为空
4、成功并返回所有信息

.

请求代码:

以前可以用NSString *currentVersionStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/lookup?bundleId=com.XXX"] encoding:NSUTF8StringEncoding error:nil];这一句直接获取版本号。现在不能这么用了,审核会被拒,改用下述方式:

/// 请求接口
- (void)requestAppVersion {

    NSString *url = @"https://itunes.apple.com/cn/lookup?bundleId=com.XXX";
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    [manager POST:url parameters:@{} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *results = [responseObject objectForKey:@"results"];
        NSDictionary *resultDic = results.firstObject;
        if (resultDic) {
            [self judgeIfNeedUpdate:resultDic];//判断是否有新的版本
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

/// 判断是否有新的版本
- (void)judgeIfNeedUpdate:(NSDictionary *)resultDic {

    //苹果商店版本号
    NSString *remoteVersion = [resultDic objectForKey:@"version"];
    NSArray *remoteArray = [remoteVersion componentsSeparatedByString:@"."];
    
    //本地版本号
    NSString *localVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
    
    BOOL ifNeedUpdate = NO;//是否需要更新版本,默认NO
    
    //版本比较:要分三位依次比较。如果只是单纯的去掉版本号中"."再两者直接比较,有些情况下结果是错的,例如1.1.0(110)和1.0.10(1010)会得出后者大于前者的错误结果。
    if (remoteArray.count >= 3 && localArray.count >= 3) {
        //比较第一位
        if ([remoteArray[0] integerValue] > [localArray[0] integerValue]) {
            ifNeedUpdate = YES;
            
        //第一位相同,比较第二位
        }else if ([remoteArray[0] integerValue] == [localArray[0] integerValue] && [remoteArray[1] integerValue] > [localArray[1] integerValue]) {
            ifNeedUpdate = YES;
            
        //第一、二位相同,比较第三位
        }else if ([remoteArray[0] integerValue] == [localArray[0] integerValue] && [remoteArray[1] integerValue] == [localArray[1] integerValue] && [remoteArray[2] integerValue] > [localArray[2] integerValue]) {
            ifNeedUpdate = YES;
        }
    }
    
    //提示用户有新版本更新,点击确认后跳转苹果商店
    if (ifNeedUpdate == YES) {
        NSString *releaseNotes = resultDic[@"releaseNotes"];//更新内容
        NSString *message = [NSString stringWithFormat:@"您有新的版本更新,更新内容:%@ \n是否现在去下载!%@", releaseNotes];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            NSString *downLoadUrl = [resultDic objectForKey:@"trackViewUrl"];//应用程序下载网址
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:downLoadUrl]];//跳转苹果商店
        }]];
        [self.navigationController presentViewController:alert animated:YES completion:nil];
    }
    
    //所有数据
    //NSString *version = resultDic[@"version"];                      //版本号
    //NSString *releaseNotes = resultDic[@"releaseNotes"];            //更新内容
    //NSString *trackViewUrl = resultDic[@"trackViewUrl"];            //应用程序下载网址
    //NSString *artistId = resultDic[@"artistId"];                    //开发者ID
    //NSString *artistName = resultDic[@"artistName"];                //开发者名称
    //NSString *releaseDate = resultDic[@"currentVersionReleaseDate"];//最新发布日期
    //NSString *minimumOsVersion = resultDic[@"minimumOsVersion"];    //最低支持系统版本
    //NSString *trackCensoredName = resultDic[@"trackCensoredName"];  //审查名称
    //NSString *trackContentRating = resultDic[@"trackContentRating"];//评级
    //NSString *trackId = resultDic[@"trackId"];                      //应用程序ID
    //NSString *trackName = resultDic[@"trackName"];                  //应用程序名称
    //NSString *ratingCountAll = resultDic[@"userRatingCount"];       //用户评论数量
    //NSString *ratingCount = resultDic[@"userRatingCountForCurrentVersion"];//当前版本的用户评论数量
    //NSArray  *screenshotUrls = resultDic[@"screenshotUrls"];        //App预览和截屏(可直接打开链接查看图片)
    //NSArray  *supportedDevices = resultDic[@"supportedDevices"];    //支持的设备型号
    //NSString *genres = resultDic[@"genres"];                        //类别(社交、娱乐、财务)
    //NSString *description = resultDic[@"description"];              //描述
    //NSString *price = resultDic[@"price"];                          //售价
    //NSString *formattedPrice = resultDic[@"formattedPrice"];        //格式化售价(没有售价时显示“免费”)
    //NSString *artistName = resultDic[@"artistName"];                //开发者
    //NSString *sellerName = resultDic[@"sellerName"];                //供应商
}

返回数据格式如下,有用的信息都在results目录下:

{
    "resultCount" : 1,
    "results" : [{
        "version" = "版本号"
        "trackViewUrl" = "应用程序下载网址",
    }]
}

参考:

iOS 利用 AppStore 版本号检测当前版本和升级
iOS APP如何实现版本检测更新
通过bundleId获取版本号等信息

如发现遗漏或者错误,请在下方评论区留言。