Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] shouldOverrideUrlLoading not working #1350

Closed
jypDev opened this issue Oct 6, 2022 · 19 comments
Closed

[Android] shouldOverrideUrlLoading not working #1350

jypDev opened this issue Oct 6, 2022 · 19 comments
Labels
bug Something isn't working

Comments

@jypDev
Copy link

jypDev commented Oct 6, 2022

Result value of requst.isForMainFrame() in shouldOverrideUrlLoading function
It returns false in chrome version 99 and above, so it can't handle intent links correctly.

I also handled NavigationActionPolicy.CANCEL in flutter

image

Environment

[✓] Flutter (Channel stable, 3.0.5, on macOS 12.5 21G72 darwin-arm, locale ko-KR)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
[✓] VS Code (version 1.70.0)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

Plugin version - 5.4.4+3

@jypDev jypDev added the bug Something isn't working label Oct 6, 2022
@github-actions
Copy link

github-actions bot commented Oct 6, 2022

👋 @jypDev

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

@khai-pham-ominext
Copy link

I also had same problem! @pichillilorenzo

  • Android webview not call shouldOverrideUrlLoading when tap a button on webview.
  • However, IOS webview is working fine.

@feduke-nukem
Copy link

same

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Oct 15, 2022

If you need to launch url with custom procotol/scheme that isn’t “http” or “https”, such as “intent”, “whatsapp” and others, you should implement it by your self, for example using the “url_launcher” plugin!

Android and iOS are different platform, so they may behave differently.
In this case, Android can’t recognise custom protocol/scheme and launch the app automatically as may do iOS. That’s something that is platform-specific.

You can check the InAppWebView code example of the repository.

@pichillilorenzo
Copy link
Owner

Ok, so, please, could give me an example code to test and the steps to reproduce the issue? Thanks!

Also, which version of the plugin are you using?

@feduke-nukem
Copy link

If you need to launch url with custom procotol/scheme that isn’t “http” or “https”, such as “intent”, “whatsapp” and others, you should implement it by your self, for example using the “url_launcher” plugin!

Android and iOS are different platform, so they may behave differently. In this case, Android can’t recognise custom protocol/scheme and launch the app automatically as may do iOS. That’s something that is platform-specific.

You can check the InAppWebView code example of the repository.

In my case useShouldOverrideUrlLoading is not firing on Android at all, while NavigateDelegate from web_view_flutter is.

@feduke-nukem
Copy link

Ok, so, please, could give me an example code to test and the steps to reproduce the issue? Thanks!

Also, which version of the plugin are you using?

I will try

The latest stable

@pichillilorenzo
Copy link
Owner

Without a code example, I can’t help you in any way! From my tests, it is working as expected

@feduke-nukem
Copy link

Without a code example, I can’t help you in any way! From my tests, it is working as expected

Is it correct?

It is only necessary to provide shouldOverrideUrlLoading function to handle navigation on every page?

Are there any conditions for it not being triggered?

@feduke-nukem
Copy link

It appeared that I was not passing useShouldOverrideUrlLoading = true into InAppWebViewOptions

Everything is working fine, thanks for replying @pichillilorenzo

@pichillilorenzo
Copy link
Owner

No problem! 👌👍

@Woojin-Crive
Copy link

Woojin-Crive commented Oct 21, 2022

it is a url to load a korea's payment security solution app.
스크린샷 2022-10-21 오후 2 47 12
스크린샷 2022-10-21 오후 2 47 31
Uri parser thorws the exception. So the code doesn't reach to the bottom's code.

            if (_webview != null && _webview!.shouldOverrideUrlLoading != null)
              return (await _webview!.shouldOverrideUrlLoading!(this, navigationAction))?.toNativeValue();
            return (await _inAppBrowser!.shouldOverrideUrlLoading(navigationAction))?.toNativeValue();

The user cannot do anything about exception handling because the shouldOverrideUrlLoading function is not called.
Temporarily, i wrapped the
NavigationAction navigationAction = NavigationAction.fromMap(arguments)!;
code with try-catch, and made a new event handler to give a user an opportunity to hanlde about the problem.

I think this should be fixed at the new version.
Korea major card company uses that intent.

And also, even if i I returned NavigationActionPolicy.CANCEL, the webview Navigates.
Why does it navigates?! =>
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
when the intent comes,
request.isForMainFrame() == false

flutter_inappwebview/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebViewClient.java
i just changed return request.isForMainFrame(); to return true;.

@pichillilorenzo

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Oct 21, 2022

@Woojin-Crive as you may have read in the code comment, there isn't any way to load an URL for a frame that is not the main frame on Android, so if the request is not for the main frame, the navigation is allowed.
So, that's why there is return request.isForMainFrame(); .
If you need to cancel navigation subframes on Android, you need to use regexToCancelSubFramesLoading option of AndroidInAppWebViewOptions, where you declare a regular expression as a string used by Java to match subframe URL loading you want to cancel.

@Woojin-Crive
Copy link

Woojin-Crive commented Oct 21, 2022

@pichillilorenzo
Oh i understood. Thank you for the reply. I appreciate it!

@pichillilorenzo
Copy link
Owner

However, I'm trying to understand how to not break dart Uri parsing with such kind of Uri coming from the Android native side.

@pichillilorenzo
Copy link
Owner

Released new version 6.0.0-beta.10 with new type WebUri to replace the Uri dart core type.
With WebUri, you have access to the raw string value used by the Uri parser.
So, if you need the unmodified string, use the WebUri.rawValue property.

For more details on how WebUri works, check the WebUri Official docs.

@MrPrakashR
Copy link

I able to call this method by setting this option in flutter_inappwebview.

var option = InAppWebViewOptions();
option.useShouldOverrideUrlLoading = true;
webViewController?.setOptions(options: InAppWebViewGroupOptions(crossPlatform: option));

@Woojin-Crive
Copy link

Using WebUri enables the developer to handle the exception of the dart uri parser!

Copy link

github-actions bot commented Oct 6, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants