如何强制用户使用 React Native 更新应用程序

Arh*_*zad 2 react-native expo

我已经在应用商店和 Play 商店更新了我的应用程序,我想强制我的应用程序用户在应用程序商店和 Playstore 中更新应用程序的新版本。

Kau*_*ik 5

您可以使用这个库react-native-appstore-version-checker 检查应用程序的 App Store / Play Store 版本 。

在 expo 应用程序中,您可以使用Constants.nativeAppVersion. 文档

现在在你的 root react native 组件中,你可以添加一个事件监听器来检测应用程序状态变化。每次应用程序从后台转换到前台时,您都可以运行您的逻辑来确定当前版本和最新版本,并提示用户更新应用程序。

 import { AppState } from 'react-native';


 class Root extends Component {

   componentDidMount() {
     AppState.addEventListener('change', this._handleAppStateChange);
   }



   _handleAppStateChange = (nextState) => {
     if (nextState === 'active') {

        /**
                Add code to check for the remote app version.
                Compare it with the local version. If they differ, i.e.,
                (remote version) !== (local version), then you can show a screen,
                with some UI asking for the user to update. (You can probably show
                a button, which on press takes the user directly to the store)
         */


     }
    }



   componentWillUnmount() {
      AppState.removeEventListener('change', this._handleAppStateChange);

   }

}

Run Code Online (Sandbox Code Playgroud)