1-min read
Trivial implementation to force update users to the latest version without having to install any number of the various paid libraries out there in React-land.
The optimal use case is when you have access to OTA and can target prior versions to upgrade backend endpoints so that everyone is pushed to the latest version. With expo this fairly easy and can be run with this command
eas update --branch production -p ios
Code snippet below -
// your various imports here
// constants
const LATEST_VERSION = '2.2.8'
const LATEST_VERSION_NUMBER = Number(STABLE_VERSION.split('.').join(''))
const appStoreName = 'gently-shop-fashion-deals'
const appStoreId = 'id6450376418'
const appStoreURL = `itms-apps://apps.apple.com/id/app/${appStoreName}/${appStoreId}?l=id`
const Root = () => {
const ver = Number(Constants.expoConfig.version.split('.').join(''))
if (ver < LATEST_VERSION_NUMBER) {
Alert.alert(
'New Update Available',
'For the best experience and new features, please update your app now.',
[{ text: 'Update Now', onPress: () => Linking.openURL(appStoreURL) }]
)
} else
return (
<SessionProvider>
<HomeStateProvider>
<GlobalStateProvider>
<StripeProvider
publishableKey={STRIPE_PUBLISHABLE_KEY}
urlScheme="gently"
merchantIdentifier="merchant.com.gently"
>
<ThemeProvider theme={theme}>
<Toast>
<App />
</ThemeProvider>
</StripeProvider>
</GlobalStateProvider>
</HomeStateProvider>
</SessionProvider>
)
}
A simple explanation of the constants for the uninitiated
LATEST_VERSION
: This will be the minimum semver in a string format. Any version below will be forced to update, i.e. a user still on v2.6.0 will see the non-dismissible Alert dialog if the latest app version is v2.8.0.appStoreName
,appStoreId
: You can find these when visiting your app URL and copying it over. Various resources available.