【问题标题】:Deep Linking not working in React Native app on Android深度链接在 Android 上的 React Native 应用程序中不起作用
【发布时间】:2022-01-19 01:54:49
【问题描述】:

我已将我的 React Native 应用程序设置为使用带有 expo-linking 的 Deep Linking,但由于某种原因,它无法在 Android 上运行(尚未在 iOS 上实现)。打开链接只是在网络浏览器中打开它,并没有按应有的方式打开应用程序。知道为什么吗?

app.json

"android": {
  "adaptiveIcon": {
    "foregroundImage": "./assets/adaptive-icon.png",
    "backgroundColor": "#FFFFFF"
  },
  "package": "com.example.myapp",
  "intentFilters": [
    {
      "action": "VIEW",
      "data": [
        {
          "scheme": "https",
          "host": "testlink.com",
        }
      ],
      "category": [
        "BROWSABLE",
        "DEFAULT"
      ]
    }
  ]
},

这并没有更新AndroidManifest,所以我手动编辑了它:

AndroidManifest.xml

<intent-filter>
  <action android:name="android.intent.action.MAIN"/>
  <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
   <action android:name="android.intent.action.VIEW"/>
   <category android:name="android.intent.category.DEFAULT"/>
   <category android:name="android.intent.category.BROWSABLE"/>
   <data android:scheme="https" android:host="testlink.com"/>
</intent-filter>

App.js

const linking = {
    prefixes: ["https://testlink.com"],    
};

useEffect(() => {
    Linking.addEventListener("url", handleDeepLink);

    return () => {
        Linking.removeEventListener("url", handleDeepLink);
    };
}, []);

return (
    <NavigationContainer /*linking={linking}*/>
        ....
    </NavigationContainer>
);

这仅适用于普通的世博会链接,但现在不起作用我想要一个自定义 URL,以便它在计算机上的网络浏览器中打开,或者在安装的应用程序上打开。

【问题讨论】:

    标签: android react-native expo android-deep-link expo-linking


    【解决方案1】:

    iOS Safari 浏览器的核心是内置深度链接,可以深度链接到自定义应用程序架构-myapp:///link-to-resources。主要在 android 上使用的基于 Chromium 的浏览器不支持 URL 输入字段中的自定义应用架构。

    解决方法是设置一个简单的网页,它可以使用浏览器 DOM 窗口 API 重定向您的应用自定义架构。

     const launchApp = (deepLink = "", fallBack = "") => {
      var now = new Date().valueOf();
      setTimeout(function () {
        if (new Date().valueOf() - now > 100) return;
        window.location = fallBack;
      }, 25);
      window.location = deepLink;
    }
    
    
     const deepLink = "myapp:///path_to_ressource/";
     const fallbackLink = "http://play.google.com/store/apps/details?id=com.yourcompany.appname"
    
    launchApp(deepLink,fallbackLink)
    
    

    【讨论】:

    • 这是否适用于我希望我的深层链接与我的后备链接相同的情况?所以,假设我拥有的链接是someexamplelink.com,如果在移动设备上按下,我想打开应用程序,或者如果在桌面上打开网站
    • 这称为通用链接,需要注册URL模式以便OS(Android或iOS)。在 Android 上很简单,但在 iOS 上,您需要在 Apple 开发者网站上证明该域的所有权 - rossbulat.medium.com/…
    • 是的,这就是我认为我用上面的代码实现的,但这在 Android 上仍然不起作用 - 我还需要你的代码才能让它工作吗?
    • Chrome 和除 safaris 以外的其他浏览器无法打开 myapp:///path_to_ressource 架构。您需要重定向到网页并使用浏览器窗口 API
    • 请随时通过 emmbyiringiro@gmail.com 安排一次简短的会议以获得进一步的帮助
    猜你喜欢
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2020-09-21
    • 2020-03-14
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多