Deep-linking not fired from WebView.

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

Deep-linking not fired from WebView

问题

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

  1. 我正在尝试从应用程序的 WebView 中触发我的Android应用程序中的深层链接但是在触发 ACTION_VIEW 意图时我收到了 `ERR_UNKNORN_URL_SCHEME` 错误该网站试图执行 `window.location.href = app://ls.mydomain.it/?key=XXX`
  2. WebView Fragment 中加载如下
  3. binding.webview.settings.javaScriptEnabled = true
  4. binding.webview.settings.loadWithOverviewMode = true
  5. binding.webview.webViewClient = object : WebViewClient() {
  6. override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
  7. return false
  8. }
  9. }
  10. binding.webview.settings.domStorageEnabled = true
  11. binding.webview.loadUrl("https://ls.mydomain.it/?deviceId=${deviceId}")
  12. 我已经在清单中添加了深层链接
  13. <activity
  14. android:name=".ui.components.home.HomeActivity"
  15. android:exported="true"
  16. android:screenOrientation="portrait"
  17. android:theme="@style/Theme.App.Starting"
  18. android:windowSoftInputMode="adjustPan"
  19. android:launchMode="singleTask"
  20. tools:ignore="LockedOrientationActivity">
  21. <intent-filter>
  22. <action android:name="android.intent.action.MAIN" />
  23. <category android:name="android.intent.category.LAUNCHER" />
  24. </intent-filter>
  25. <intent-filter>
  26. <action android:name="android.intent.action.VIEW" />
  27. <category android:name="android.intent.category.DEFAULT" />
  28. <category android:name="android.intent.category.BROWSABLE" />
  29. <data android:scheme="app" android:host="ls.mydomain.it" />
  30. </intent-filter>
  31. </activity>
  32. 然后在 Activity 中覆盖了意图
  33. override fun onNewIntent(intent: Intent?) {
  34. super.onNewIntent(intent)
  35. if (intent?.action == Intent.ACTION_VIEW) {
  36. val data: Uri? = intent.data
  37. newLicense(data)
  38. }
  39. }

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:

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

I have added the deep-linking in the Manifest:

  1. &lt;activity
  2. android:name=&quot;.ui.components.home.HomeActivity&quot;
  3. android:exported=&quot;true&quot;
  4. android:screenOrientation=&quot;portrait&quot;
  5. android:theme=&quot;@style/Theme.App.Starting&quot;
  6. android:windowSoftInputMode=&quot;adjustPan&quot;
  7. android:launchMode=&quot;singleTask&quot;
  8. tools:ignore=&quot;LockedOrientationActivity&quot;&gt;
  9. &lt;intent-filter&gt;
  10. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  11. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  12. &lt;/intent-filter&gt;
  13. &lt;intent-filter&gt;
  14. &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
  15. &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
  16. &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
  17. &lt;data android:scheme=&quot;app&quot; android:host=&quot;ls.mydomain.it&quot; /&gt;
  18. &lt;/intent-filter&gt;
  19. &lt;/activity&gt;

And then override the intent in the Activity:

  1. override fun onNewIntent(intent: Intent?) {
  2. super.onNewIntent(intent)
  3. if (intent?.action == Intent.ACTION_VIEW) {
  4. val data: Uri? = intent.data
  5. newLicense(data)
  6. }
  7. }

答案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:

  1. binding.webview.webViewClient = object : WebViewClient() {
  2. override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
  3. val url = request?.url.toString()
  4. if (url.startsWith("app://")) {
  5. try {
  6. val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
  7. addCategory(Intent.CATEGORY_BROWSABLE)
  8. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
  9. flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER or
  10. Intent.FLAG_ACTIVITY_REQUIRE_DEFAULT
  11. }
  12. }
  13. startActivity(intent)
  14. } catch (e: ActivityNotFoundException) {
  15. showToast("Non è stato possibile attivare la licenza, contattare l'assistenza")
  16. }
  17. return true
  18. }
  19. return false
  20. }
  21. }

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:

  1. binding.webview.webViewClient = object : WebViewClient() {
  2. override fun shouldOverrideUrlLoading(view: WebView?,request: WebResourceRequest?): Boolean {
  3. val url = request?.url.toString()
  4. if (url.startsWith(&quot;app://&quot;)) {
  5. try {
  6. val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
  7. addCategory(Intent.CATEGORY_BROWSABLE)
  8. if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.R) {
  9. flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER or
  10. Intent.FLAG_ACTIVITY_REQUIRE_DEFAULT
  11. }
  12. }
  13. startActivity(intent)
  14. } catch (e: ActivityNotFoundException) {
  15. showToast(&quot;Non &#232; stato possibile attivare la licenza, contattare l&#39;assistenza&quot;)
  16. }
  17. return true
  18. }
  19. return false
  20. }
  21. }

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:

确定