簡體   English   中英

深度鏈接在 Android 上的 React Native 應用程序中不起作用

[英]Deep Linking not working in React Native app on Android

我已經將我的 React Native 應用程序設置為使用帶有 expo-linking 的 Deep Linking,但由於某種原因,它在 Android 上不起作用(尚未在 iOS 上實現)。 打開鏈接只是在 web 瀏覽器中打開它,並沒有按應有的方式打開應用程序。 知道為什么嗎?

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>

應用程序.js

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

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

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

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

這僅適用於普通的 expo 鏈接,但現在不起作用我想要一個自定義的 URL 以便它在 web 瀏覽器中打開,如果它安裝在計算機上或應用程序上。

在核心,iOS Safari 瀏覽器具有內置的深度鏈接,可以深度鏈接到自定義應用程序架構-myapp myapp:///link-to-resources 主要在 android 上使用的基於 Chromium 的瀏覽器不支持URL 輸入字段中的自定義應用程序架構。

解決方法是設置簡單的web 頁面,該頁面可以使用瀏覽器 DOM Window 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM