Skip to content

Instantly share code, notes, and snippets.

@richardgill
Created March 16, 2017 14:42
Show Gist options
  • Save richardgill/141feeccaca79f37cf1882343e9b7391 to your computer and use it in GitHub Desktop.
Save richardgill/141feeccaca79f37cf1882343e9b7391 to your computer and use it in GitHub Desktop.
Forcing React Native App Upgrades
import { Alert, Linking } from 'react-native'
import DeviceInfo from 'react-native-device-info'
import config from 'expresso-common/config'
import { platformIsIos } from 'expresso-common/common/platform'
import Firebase from 'firebase'
import semanticVersion from 'semver'
const attemptAndroidUpgrade = (appId) => {
const playStoreUri = `market://details?id=${appId}`
const playStoreUrl = `https://play.google.com/store/apps/details?id=${appId}`
Linking.canOpenURL(playStoreUri).then((supported) => {
if (supported) {
Linking.openURL(playStoreUri)
} else {
Linking.openURL(playStoreUrl)
}
})
}
const attemptIosUpgrade = (appId) => {
// failover if itunes - a bit excessive
const itunesURI = `itms-apps://itunes.apple.com/app/${appId}?mt=8`
Linking.canOpenURL(itunesURI).then((supported) => {
if (supported) {
Linking.openURL(itunesURI)
}
})
}
const attemptUpgrade = (appId) => {
if (platformIsIos()) {
attemptIosUpgrade(appId)
} else {
attemptAndroidUpgrade(appId)
}
}
const showUpgradePrompt = (appId) => {
Alert.alert(
'You must upgrade',
'There is an updated version available.',
[
{
text: 'Upgrade',
onPress: () => {
attemptUpgrade(appId)
}
},
],
)
}
// Our app doesn't have 2 .'s just 1 . So we must add a fake . to compare the versions.
const isVersionOk = (minimumAllowedVersion, currentVersion) => {
return semanticVersion.gte(`${currentVersion}.0`, `${minimumAllowedVersion}.0`)
}
export const checkVersionAndForceUpgrade = () => {
const minimumAllowedVersionRef = `${config.firebaseUrl}${config.minimumAllowedVersionRef}`
const appIdRef = `${config.firebaseUrl}${config.appIdRef}`
const minAllowedRef = new Firebase(minimumAllowedVersionRef)
minAllowedRef.on('value', (minimumAllowedVersion) => {
if (!isVersionOk(minimumAllowedVersion.val(), DeviceInfo.getVersion())) {
new Firebase(appIdRef).once('value').then((appId) => showUpgradePrompt(appId.val()))
}
}, (error) => console.log(error))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment