You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Expo版React Native中获取Play Store应用版本?

解决Play Store版本获取与应用更新提示问题

1. 修正react-native-version-check的使用方式

你之前可能没正确配置Play Store相关参数,导致只能拿到本地版本。按以下步骤调整:

  • 确保AndroidManifest.xml中添加网络权限:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • 调用getLatestVersion时指定Play Store作为数据源:
    import VersionCheck from 'react-native-version-check';
    
    const checkForUpdate = async () => {
      try {
        const latestVersion = await VersionCheck.getLatestVersion({
          provider: 'playStore',
          appID: 'com.your.app.package', // 替换为你的应用包名
        });
        const currentVersion = VersionCheck.getCurrentVersion();
        
        // 版本对比逻辑(注意版本号格式要统一,比如都是x.y.z)
        if (latestVersion > currentVersion) {
          // 这里写你的更新弹窗逻辑
          alert(`发现新版本 ${latestVersion},请前往Play Store更新`);
        }
      } catch (error) {
        console.error('获取最新版本失败:', error);
      }
    };
    
    // 在合适的时机调用,比如APP启动时
    checkForUpdate();
    
    注:该库通过爬取Play Store公开页面获取版本,若遇到地区限制或页面结构变更可能失效,可尝试下方方案。

2. 自行实现Play Store版本爬取

如果第三方库失效,可直接请求Play Store应用页面并解析版本号:

import { getCurrentVersion } from 'react-native-version-check';

const fetchPlayStoreVersion = async (packageName) => {
  try {
    const response = await fetch(`https://play.google.com/store/apps/details?id=${packageName}&hl=en`);
    const html = await response.text();
    // 匹配版本号的正则(Play Store页面结构可能变动,需按需调整)
    const versionRegex = /Current Version<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(.*?)<\/span>/;
    const match = html.match(versionRegex);
    
    if (match && match[1]) {
      return match[1].trim();
    }
    throw new Error('未找到版本信息');
  } catch (err) {
    console.error('爬取版本失败:', err);
    return null;
  }
};

// 使用示例
fetchPlayStoreVersion('com.your.app.package').then(latestVersion => {
  if (latestVersion) {
    const currentVersion = getCurrentVersion();
    if (latestVersion > currentVersion) {
      // 展示更新提示
    }
  }
});

3. 稳定方案:借助后端调用Google Play Developer API

如果需要长期稳定的版本获取,建议通过后端服务调用官方API:

  • 后端创建Google Play开发者服务账号,获取认证凭据
  • 后端调用edits.tracks.get接口,获取生产渠道的最新版本号
  • 客户端请求自己的后端接口获取版本信息,再做对比
    这种方式不受页面结构变更影响,但需要额外的后端开发工作。

4. 备选库:react-native-app-update

该库集成了版本检测和更新弹窗,配置更简便:

import AppUpdate from 'react-native-app-update';

const checkUpdate = () => {
  AppUpdate.checkUpdate({
    appKey: 'com.your.app.package',
    isDebug: false,
  }).then(res => {
    if (res.shouldUpdate) {
      AppUpdate.showUpdateDialog({
        title: '新版本可用',
        description: res.updateDescription,
        cancelButtonText: '稍后再说',
        confirmButtonText: '立即更新',
      });
    }
  });
};

内容的提问来源于stack exchange,提问作者Henrique Matheus Roos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 07:07:33