如何在 WebView 应用程序上启用深层链接

时间:2021-01-05 20:08:50

标签: android webview android-webview

我的应用程序是一个 WebView 应用程序,我已经设置了 URL 意图过滤器,因此当点击域“riftacademy.org”的链接时,它会打开我的应用程序。

现在我正在尝试在 WebView 应用程序中实现深层链接...

我在这里的帖子中看到了类似的内容How to enable deep-linking in WebView on Android app?

但与上面使用自定义 URI 方案打开应用程序不同,我想使用我的域名“riftacademy.org”在我的 WebView 中实现深层链接

在这种情况下,当点击 https://riftacademy.org/loginhttps://riftacademy.org/register 等链接时,它应该打开应用程序并转到与链接匹配的页面。但在这种情况下,它打开应用程序但只加载主页 riftacademy.org...

我希望 WebView 应用能够在应用打开后加载链接,类似于 YouTube 和 Facebook 处理链接的方式

WebView 中可以使用域名实现深度链接吗?

我猜测可能需要编辑的片段

newWebView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW);
                        browserIntent.setData(Uri.parse(url));
                        startActivity(browserIntent);
                        return true;
                    }
                });
                return true;
            }
        });

提前致谢

1 个答案:

答案 0 :(得分:0)

所以我已经能够在 https://webintoapp.com 的 Ron 的帮助下解决这个问题......这是使用 URI 意图过滤器并将以下代码放置在 MainActivity.java 和 AndroidManifest.xml 中实现的

我将此添加到 MainActivity.java 中的 OnCreate 方法...网站名称应添加在 else{mWebView.loadUrL

之后
SetWebView(mWebView);
        Intent intent = getIntent();
        Uri data = intent.getData();
        if(data != null) {
            //Load the deep-link url:
            String url = intent.getDataString();
            mWebView.loadUrl(url);
        }
        else
        {
            mWebView.loadUrl("https://riftacademy.org/");
        }

这对我的 MainActivity.xml...android:host 也应该是网站 URL

<activity android:name=".MainActivity">
            <intent-filter android:label="The Title">
                <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="riftacademy.org" />
            </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="http"
                    android:host="riftacademy.org" />

这将使 WebView 应用程序能够加载所有类型的链接,只要这些链接以网站的​​默认 URL 开头