Deep-linking not fired from WebView.

huangapple go评论65阅读模式
英文:

Deep-linking not fired from WebView

问题

I understand your request. Here is the translation of the code you provided:

我正在尝试从应用程序的 WebView 中触发我的Android应用程序中的深层链接但是在触发 ACTION_VIEW 意图时我收到了 `ERR_UNKNORN_URL_SCHEME` 错误该网站试图执行 `window.location.href = app://ls.mydomain.it/?key=XXX`。

WebView 在 Fragment 中加载如下

        binding.webview.settings.javaScriptEnabled = true
        binding.webview.settings.loadWithOverviewMode = true
        binding.webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                return false
            }
        }
        binding.webview.settings.domStorageEnabled = true
        binding.webview.loadUrl("https://ls.mydomain.it/?deviceId=${deviceId}")

我已经在清单中添加了深层链接

        <activity
            android:name=".ui.components.home.HomeActivity"
            android:exported="true"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.App.Starting"
            android:windowSoftInputMode="adjustPan"
            android:launchMode="singleTask"
            tools:ignore="LockedOrientationActivity">
            <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="app" android:host="ls.mydomain.it" />
            </intent-filter>
        </activity>

然后在 Activity 中覆盖了意图

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)

        if (intent?.action == Intent.ACTION_VIEW) {
            val data: Uri? = intent.data
            newLicense(data)
        }
    }

Is there anything else you would like to know or clarify?

英文:

I'm trying to fire the deep-linking in my Android application from the application WebView but instead of firing the ACTION_VIEW intent i get ERR_UNKNORN_URL_SCHEME, the website tries to make a window.location.href = app://ls.mydomain.it/?key=XXX.

The WebView is load in a Fragment like this:

    binding.webview.settings.javaScriptEnabled = true
    binding.webview.settings.loadWithOverviewMode = true
    binding.webview.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?,request: WebResourceRequest?): Boolean {
            return false
        }
    }
    binding.webview.settings.domStorageEnabled = true
    binding.webview.loadUrl(&quot;https://ls.mydomain.it/?deviceId=${deviceId}&quot;)

I have added the deep-linking in the Manifest:

    &lt;activity
        android:name=&quot;.ui.components.home.HomeActivity&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;
        android:theme=&quot;@style/Theme.App.Starting&quot;
        android:windowSoftInputMode=&quot;adjustPan&quot;
        android:launchMode=&quot;singleTask&quot;
        tools:ignore=&quot;LockedOrientationActivity&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;

            &lt;data android:scheme=&quot;app&quot; android:host=&quot;ls.mydomain.it&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;

And then override the intent in the Activity:

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    if (intent?.action == Intent.ACTION_VIEW) {
        val data: Uri? = intent.data
        newLicense(data)
    }
}

答案1

得分: 0

I've solved the issue by calling the startActivity with ACTION_VIEW intent which contains the deep-link URI by overriding the shouldOverrideUrlLoading

The WebViewClient looks like this now:

binding.webview.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        val url = request?.url.toString()
        if (url.startsWith("app://")) {
            try {
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
                    addCategory(Intent.CATEGORY_BROWSABLE)
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER or
                                Intent.FLAG_ACTIVITY_REQUIRE_DEFAULT
                    }
                }
                startActivity(intent)
            } catch (e: ActivityNotFoundException) {
                showToast("Non è stato possibile attivare la licenza, contattare l'assistenza")
            }
            return true
        }

        return false
    }
}

With FLAG_ACTIVITY_REQUIRE_NON_BROWSER and FLAG_ACTIVITY_REQUIRE_DEFAULT flags, the intent will match only the URI that matches the application deep-linking.

Source

英文:

I've solved the issue by calling the startActivity with ACTION_VIEW intent which contains the deep-link URI by overriding the shouldOverrideUrlLoading

The WebViewClient looks like this now:

    binding.webview.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?,request: WebResourceRequest?): Boolean {
            val url = request?.url.toString()
            if (url.startsWith(&quot;app://&quot;)) {
                try {
                    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
                        addCategory(Intent.CATEGORY_BROWSABLE)
                        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.R) {
                            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER or
                                    Intent.FLAG_ACTIVITY_REQUIRE_DEFAULT
                        }
                    }
                    startActivity(intent)
                } catch (e: ActivityNotFoundException) {
                    showToast(&quot;Non &#232; stato possibile attivare la licenza, contattare l&#39;assistenza&quot;)
                }
                return true
            }

            return false
        }
    }

With FLAG_ACTIVITY_REQUIRE_NON_BROWSER and FLAG_ACTIVITY_REQUIRE_DEFAULT flags the intent will match only the URI that matches the application deep-linking.

Source

huangapple
  • 本文由 发表于 2023年4月19日 15:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定