maui: Android WebViewClient's ShouldInterceptRequest is never called in MAUI WebView

Description

Android WebViewClient’s ShouldInterceptRequest is never called in MAUI WebView

Steps to Reproduce

1.create a maui demo; 2.copy the following code to MauiProgram.cs:

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
			.ConfigureFonts(fonts =>
			{
				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			})
			.ConfigureMauiHandlers(handlers =>
			{
				handlers.AddHandler<Microsoft.Maui.Controls.WebView, ProblemHandler2>();
			});

		return builder.Build();
	}
}

internal class ProblemHandler1 : WebViewHandler
{

	protected override Android.Webkit.WebView CreatePlatformView()
	{
		var wv = base.CreatePlatformView();
		wv.SetWebViewClient(new CustomWebClient());

		return wv;
	}

}

internal class ProblemHandler2 : WebViewHandler
{

    protected override Android.Webkit.WebView CreatePlatformView()
    {
        var wv = new Android.Webkit.WebView(Android.App.Application.Context);
        wv.SetWebViewClient(new CustomWebClient());

        return wv;
    }

}

internal class CustomWebClient : WebViewClient
{

    public override WebResourceResponse ShouldInterceptRequest(Android.Webkit.WebView view, IWebResourceRequest request)
    {
        Debugger.Break();
        Debug.WriteLine(request.Url.ToString());

        return base.ShouldInterceptRequest(view, request);
    }

}

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();

        WebViewHandler.Mapper.AppendToMapping("MyHandler", (handler, view) =>
        {
#if ANDROID
			var xWv = handler.PlatformView;

			// For ProblemHandler2, this is needed to actually navigate:
			xWv.LoadUrl("https://www.google.com/");
#endif
        });

        this.wv.Source = "https://www.google.com/";
	}

}

3.build and deploy this demo to android device. (I only tested on android 13 emulator )

Link to public reproduction project repository

https://github.com/datvm/DemoMauiWvClient

Version with bug

Unknown/Other (please specify)

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 13

Did you find any workaround?

No response

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 25 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Still no reply from the team about this. It would be nice to at least get a rough timeline for a fix. 1 month, 6 months, longer??

WebViewClient.OnPageFinished(Android.Webkit.WebView platformView, string url) is NOT being called as well.

UPDATE: I have managed to get the work-around functional! I changed my custom WebViewClient to inherit from MauiWebViewClient (instead of WebViewClient) and updated the code in my MauiAppBuilderExtensions to be:

Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping(
            nameof(global::Android.Webkit.WebViewClient),
            (handler, _) => handler.PlatformView.SetWebViewClient(new DweWebViewClient((Microsoft.Maui.Handlers.WebViewHandler)handler)));

This seems to work as the OnReceivedSslError() method is now being hit and the WebView.Navigated event is being fired too.

NOTE: This AppendToMapping work-around also seems to work for SetWebChromeClient() as that is now working for me too

Any news about this? More than a month passed and still no clues about when this will be fixed

I’m being blocked to ship my app too. Any news @jsuarezruiz or @hartez @StephaneDelcroix @PureWeen @rmarinho @davidortinau @maddymontaquila about this?

@hartez @davidortinau @jsuarezruiz @maddymontaquila any due date about this? It totally prevents me to ship my MAUI app without this fixed.

@rmarinho I’ve tried also on my side the suggested workaround and as said by @mihaly-bence16 the signature of the method is different. Even so it does not work. So any updates regarding this?

Btw in the mean time, can anyone check their source code and see how they fix it for their MAUI Blazor? I think their MAUI Blazor uses a customized WebView as well and intercept the https://0.0.0.0. Maybe we can use that workaround in the mean time?

UPDATE: It’s here, they use WebChromeClient so it’s irrelevant they use a custom WebViewHandler defined here WebViewHandler.Android.cs. No idea why it works for them though. Can someone investigate?

Any news about it? This is a very big issue, we cannot ship the app without this fix.

Hi @jessiezh0320 @hartez Would you have any idea when this is likely to be fixed, or a workaround? Thank you

Hi any update on this? This is very important because without it, we cannot load local HTML content in our app following the official Android guide

@ederbond you are correct. This issue was created from this question on StackOverflow (by me), and I tried a few other callbacks as well. I suspect none of them is called.

Hi @hartez , is there any workaround ?