检查是否可以打开外部应用程序并使用React Native打开它

问题描述 投票:0回答:2

我有一个第三方应用程序,显然有一些深层链接协议(

x-mythirdpartyapp://
)。我不需要对其内容进行任何深层链接,只需打开它即可。它也不会对相应的网址做出反应(即https://mythirdpartyapp.com),因此与whatsapp相关的类似问题的答案在这里毫无用处。

根据我迄今为止的研究,这些自定义深度链接协议仅适用于 iOS,因此这应该适用于 iOS

Linking.canOpenUrl("mythirdpartyapp://")

and

Linking.openUrl("mythirdpartyapp://")

但无法确认,因为我没有任何 iOS 设备。

对于 Android,您显然可以使用

Linking.sendIntent
打开意图,但这还不够,因为在我的用例中,我需要知道在实际发送意图之前是否可以发送意图。

android ios react-native deep-linking
2个回答
0
投票

这些实际上可以在 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}`
    )
  }
}

0
投票
  1. 检查时间阈值

    • 如果应用程序已安装,时间差通常会大于1秒(当应用程序进入后台时)。
    • 如果应用程序未安装,时间差将小于1秒(因为应用程序保持在前台)。
  2. 配置深层链接:

    • Android:使用意图过滤器确保
      AndroidManifest.xml
      中的正确配置。
    • iOS:使用 URL 方案或通用链接,并在 Info.plist 和通用链接的
      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');
    }
  }
}, []);
© www.soinside.com 2019 - 2024. All rights reserved.