Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebViewClient's ShouldInterceptRequest is never called in MAUI WebView

UPDATE: it's a confirmed bug. Please upvote it here because it doesn't really receive a lot of attention from MS.


I need to override the shouldInterceptRequest method of WebViewClient to load in-app HTML content following that guide.

Here's the repo with the reproducible code: GitHub. I took a sample code from MS Q&A as well:

// ...
.ConfigureMauiHandlers(handlers =>
{
    handlers.AddHandler<Microsoft.Maui.Controls.WebView, ProblemHandler2>();
});
// ...

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;
    }

}

In the repo, I included 2 custom handlers:

  • ProblemHandler2 is the exact snippet by the MSFT. I realized a problem: Setting MAUI WebView's Source property no longer navigates the real Android WebView:
        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/";
  • ProblemHandler1 uses the default result and adds a custom handler. This fixes the navigation problem, but, both problem have the same issue:

ShouldInterceptRequest is never called. It is never called on anything even when I manually click a link to navigate. What am I missing? I am sure the CustomWebClient is correctly created and set.

I noticed none of the other callbacks works as well, for example:

    public override void OnPageStarted(Android.Webkit.WebView view, string url, Bitmap favicon)
    {
        Debugger.Break();
        Debug.WriteLine(url);

        base.OnPageStarted(view, url, favicon);
    }

    public override void OnPageFinished(Android.Webkit.WebView view, string url)
    {
        Debugger.Break();
        Debug.WriteLine(url);

        base.OnPageFinished(view, url);
    }

I also tried using WebViewHandler.Mapping but it also does not work:

        WebViewHandler.Mapper.AppendToMapping("MyHandler", (handler, _) =>
        {
#if ANDROID
            handler.PlatformView.SetWebViewClient(new CustomWebClient());
#endif
        });
like image 552
Luke Vo Avatar asked Nov 19 '25 15:11

Luke Vo


1 Answers

I could be wrong but, I think this might have to do with your overridden version of the CreatePlatform method,

Can you try what the default WebViewHandler is doing:

protected override WebView CreatePlatformView()
{
        var platformView = new MauiWebView(this, Context!)
        {
            LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent)
        };

        platformView.Settings.JavaScriptEnabled = true;
        platformView.Settings.DomStorageEnabled = true;
        platformView.Settings.SetSupportMultipleWindows(true);

        return platformView;
}

Check this URL for the default handlers CreatePlatform setup :

https://github.com/dotnet/maui/blob/c6250a20d73e1992b4a02e6f3c26a1e6cbcbe988/src/Core/src/Handlers/WebView/WebViewHandler.Android.cs

Also don't use Application Context in Handlers, Handlers have their own Context property you can use.

like image 183
FreakyAli Avatar answered Nov 21 '25 05:11

FreakyAli