question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android WebViewClient's ShouldInterceptRequest is never called in MAUI WebView

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:5
  • Comments:25 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
CartBlanchecommented, Dec 21, 2022

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??

5reactions
ederbondcommented, Nov 3, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android WebViewClient's ShouldInterceptRequest is never ...
I realized a problem: Setting MAUI WebView 's Source property no longer navigates the real Android WebView: WebViewHandler.Mapper.
Read more >
How to set custom WebViewClient on Android handler
I am working on brand new Maui app, and would like to know how I can use a custom WebViewClient on Android, using...
Read more >
Intercept all requests in WebView on Android | by Andy Wang
I have a requirement that all HTTP requests from a WebView on Android need to be handled locally. For example, providing assets for...
Read more >
WebViewClient | Android Developers
Quickly bring your app to life with less code, using a modern declarative approach to UI, and the simplicity of Kotlin. ... Start...
Read more >
JS/.NET interact on MAUI WebView : r/dotnetMAUI
NET interactions in a WebView on both Android and iOS. ... I tried to built it with handlers, but than the OnPageFinished is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found