我有一个第三方应用程序,显然有一些深层链接协议(
x-mythirdpartyapp://
)。我不需要对其内容进行任何深层链接,只需打开它即可。它也不会对相应的网址做出反应(即https://mythirdpartyapp.com),因此与whatsapp相关的类似问题的答案在这里毫无用处。
根据我迄今为止的研究,这些自定义深度链接协议仅适用于 iOS,因此这应该适用于 iOS
Linking.canOpenUrl("mythirdpartyapp://")
and
Linking.openUrl("mythirdpartyapp://")
但无法确认,因为我没有任何 iOS 设备。
对于 Android,您显然可以使用
Linking.sendIntent
打开意图,但这还不够,因为在我的用例中,我需要知道在实际发送意图之前是否可以发送意图。
这些实际上可以在 iOS 上使用,这对 Android 来说是有效的:https://github.com/lucasferreira/react-native-send-intent
这是完整的解决方案:
async function canOpenApp() {
if (Platform.OS === "android") {
return SendIntentAndroid.isAppInstalled(ANDROID_ID)
} else {
return Linking.canOpenURL(URL_PROTOCOL)
}
}
async function openApp() {
if (Platform.OS === "android") {
return SendIntentAndroid.openApp(ANDROID_ID, {})
} else {
return Linking.openURL(URL_PROTOCOL)
}
}
async function openStore() {
if (Platform.OS === "android") {
return Linking.openURL(
`https://play.google.com/store/apps/details?id=${ANDROID_ID}`
)
} else {
return Linking.openURL(
`https://apps.apple.com/${APP_STORE_LOCALE}/app/${APP_STORE_NAME}/id${APP_STORE_ID}`
)
}
}
检查时间阈值:
配置深层链接:
AndroidManifest.xml
中的正确配置。apple-app-site-association
文件中进行正确配置。这是 JavaScript 中的示例实现:
const getIsAppInstalledByDeeplink = useCallback(async (url) => {
if (url) {
const startTime = Date.now();
Linking.openURL(url);
await new Promise((resolve) => setTimeout(resolve, 1000));
const endTime = Date.now();
if (endTime - startTime <= 1100) {
console.log('App does not exist');
} else {
console.log('App exists and was opened');
}
}
}, []);