pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.27k stars 1.61k forks source link

[Android] shouldOverrideUrlLoading removes headers no matter flutter returned NavigationActionPolicy.ALLOW or CANCEL #2247

Open rikucherry1993 opened 2 months ago

rikucherry1993 commented 2 months ago

Environment

Technology Version
Flutter version 3.13.8
Plugin version 6.0.0
Android version 33
iOS version
macOS version
Xcode version
Google Chrome version

Device information:

Description

This is more of a question regarding the implementation.

When toggle on the flag useShouldOverrideUrlLoading in InAppWebViewSettings, on the Android side implementation of the plugin it goes to: (InAppWebViewClientCompat::shouldOverrideUrlLoading) https://github.com/pichillilorenzo/flutter_inappwebview/blob/751a104716b2c5158b5cad58347430d01965f00d/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClientCompat.java#L76-L101

According to the above logic, as long as the request.isForMainFrame() is true shouldOverrideUrlLoading always returns true, which causes the webView stops loading current url. This is regardless of whether flutter side returns ALLOW or CANCEL.

Then in the case that flutter returns ALLOW, the WebViewClient called webview.loadUrl() to manually load same url: https://github.com/pichillilorenzo/flutter_inappwebview/blob/v6.0.0/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClientCompat.java#L117-L128 This actually overwrites the original request with a new request, which no longer contains the original headers information.

But this is agianst the [Android documentation](https://developer.android.com/reference/android/webkit/WebViewClient?_gl=1*f2qg76*_up*MQ..*_ga*OTAyMzAzMi4xNzIzMDAwOTkx*_ga_6HH9YJMN9M*MTcyMzAwMDk5MS4xLjAuMTcyMzAwMDk5Ni4wLjAuMA..#shouldOverrideUrlLoading(android.webkit.WebView,%20android.webkit.WebResourceRequest) :

Note: Do not call [WebView#loadUrl(String)](https://developer.android.com/reference/android/webkit/WebView#loadUrl(java.lang.String)) with the request's URL and then return true. This unnecessarily cancels the current load and starts a new load with the same URL. The correct way to continue loading a given URL is to simply return false, without calling [WebView#loadUrl(String)](https://developer.android.com/reference/android/webkit/WebView#loadUrl(java.lang.String)).

if it's ALLOW, shouldn't it just let the shouldOverrideUrlLoading method returns false so the webview can continue to load same url? Why it returns true then reload the url itself?

As a result the request header is always missing as long as it went through the shouldOverrideUrlLoading.

Expected behavior:

Current behavior:

Steps to reproduce

Images

Stacktrace/Logcat

github-actions[bot] commented 2 months ago

👋 @rikucherry1993

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!