Created
September 29, 2018 03:33
-
-
Save giautm/b822617ab3c8479de54b7c28971c3094 to your computer and use it in GitHub Desktop.
Modified version from https://github.com/FiberJW/react-native-app-link/blob/master/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Linking, Platform } from 'react-native' | |
export const maybeOpenURL = async ( | |
url, | |
config, | |
) => { | |
Linking.openURL(url).catch((err) => { | |
if (err.code === 'EUNSPECIFIED') { | |
openInStore({ | |
appStoreLocale: 'us', | |
...config, | |
}) | |
} else { | |
throw new Error(`Could not open ${config.appName}. ${err.toString()}`) | |
} | |
}) | |
} | |
export const openInStore = async ({ appName, appStoreId, appStoreLocale = 'us', playStoreId }) => { | |
if (Platform.OS === 'ios') { | |
Linking.openURL( | |
`https://itunes.apple.com/${appStoreLocale}/app/${appName}/id${appStoreId}`, | |
) | |
} else { | |
Linking.openURL( | |
`https://play.google.com/store/apps/details?id=${playStoreId}`, | |
) | |
} | |
} | |
export default { | |
maybeOpenURL, | |
openInStore, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment