检测苹果商店App版本

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


和后台的同事定义好维护一个版本升级接口,每次发布新版本后在管理平台更新一下版本信息,移动端通过请求接口获取最新版本信息来提示用户是否需要升级。这种方式方便控制是否强制升级等自定义操作。


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


有下面几个请求链接: (链接中的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、成功并返回所有信息


请求代码:

/// 请求接口

- (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:@"."];


    //版本比较:要分三位依次比较,如果只是单纯的去掉版本号中"."再直接比较,有些情况下结果是错的,例如1.1.0和1.0.10

    BOOL ifNeedUpdate = NO;//是否需要更新版本,默认NO

    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) {

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" 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]];//跳转苹果商店

        }]];

        [[DTContextGet() navigationController] presentViewController:alert animated:YES completion:nil];

    }


    //所有数据

    //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 *trackViewUrl = resultDic[@"trackViewUrl"];            //应用程序下载网址

    //NSString *ratingCountAll = resultDic[@"userRatingCount"];      //用户评论数量

    //NSString *ratingCount = resultDic[@"userRatingCountForCurrentVersion"];//当前版本的用户评论数量

    //NSString *version = resultDic[@"version"];                      //版本号

    //NSArray  *screenshotUrls = resultDic[@"screenshotUrls"];        //App预览和截屏(可直接打开链接查看图片)

    //NSArray  *supportedDevices = resultDic[@"supportedDevices"];    //支持的设备型号

}


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

{

    "resultCount" : 1,

    "results" : [{

        "version" = "版本号"

        "trackViewUrl" = "应用程序下载网址",

    }]

}


参考:

iOS 利用 AppStore 版本号检测当前版本和升级

iOS APP如何实现版本检测更新

通过bundleId获取版本号等信息


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

你可能感兴趣的:(检测苹果商店App版本)