Skip to content

No way to catch permissions requests from new .NET 9 HybridWebView? Old method doesn't work anymore. #25784

@jonmdev

Description

@jonmdev

Description

Instructions for catching and forwarding permissions request (eg. camera) in WebView are given here:

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/webview?view=net-maui-8.0&pivots=devices-android

HybridWebView used to derive from it (not sure now) so it worked the same way.

They suggest creating a MyWebChromeClient and then setting this on the platform view of the WebView in Android:

var webViewHandler = (IWebViewHandler)webView.Handler;
var chromeClient = new MauiAppWebViewHandlers.Platforms.Android.MyWebChromeClient(webViewHandler);
webViewHandler.PlatformView.SetWebChromeClient(chromeClient);

However, if you try this, it does not work in .NET Maui with HybridWebView. It previously worked fine in .NET 8. Now you are told:

**System.InvalidCastException:** 'Specified cast is not valid.'

Regarding conversion of the webView handler to IWebViewHandler. Thus we cannot create the MyWebChromeClient according to these directions.

What might be the fix? @Eilon, I know this is your baby. Any thoughts or solution? Thanks.

Steps to Reproduce

Run the demo project in Android and see the error that returns.

Demo project was made by just two things: (1) Copy and paste the demo MyWebChromeClient code into ChromeClient.cs, and (2) Copy and paste the following in to App.xaml.cs:

        protected override Window CreateWindow(IActivationState? activationState)
        {
            ContentPage mainPage = new();
            HybridWebView webView = new();
            mainPage.Content = webView;
#if ANDROID

            webView.HandlerChanged += delegate {

                var platformView = webView.ToPlatform(webView.Handler.MauiContext);
                Debug.WriteLine("PLATFORM VIEW " + platformView.GetType());//Microsoft.Maui.Platform.MauiHybridWebView
                Debug.WriteLine("HANDLER PLATFORM VIEW " + webView.Handler.PlatformView.GetType()); //Microsoft.Maui.Platform.MauiHybridWebView
                Debug.WriteLine("HANDLER " + webView.Handler.GetType());//Microsoft.Maui.Handlers.HybridWebViewHandler
                var webViewHandler = (IWebViewHandler)webView.Handler;
                Debug.Assert(webViewHandler != null, "Value must not be null.");
                var chromeClient = new MauiAppWebViewHandlers.Platforms.Android.MyWebChromeClient(webViewHandler);
                webViewHandler.PlatformView.SetWebChromeClient(chromeClient);
            };


#endif
            return new Window(mainPage);
        }

Link to public reproduction project repository

https://github.com/jonmdev/HybridWebViewPermissionsBug

Version with bug

9.0.0-rc.1.24453.9

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.0-preview.1.7762

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

None

Relevant log output

**System.InvalidCastException:** 'Specified cast is not valid.'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions