webviewpermissionsmaui

How to fix double permission popup in MAUI ios web view


I have a Maui App and i'm trying to open my streaming website inside the WebView.

But everytime i open my streaming website inside Maui WebView ,the website is asking for camera permission "so annoying for the user" (even if the user already granted camera access to my app in the first time i installed it).

My question ,is there any way to disable the permission prompt inside the WebView website and make the website uses the given permission of the app itself.

did anyone face a similar problem ?

thanks for your help.


Solution

  • Assume that you are using getUserMedia() to get camera. You can set the WKUIDelegate for the webview in iOS.

    public class CustomWebViewDelegate :WKUIDelegate
           {
             [Export("webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:")]
            public override void RequestMediaCapturePermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, WKMediaCaptureType type,Action<WKPermissionDecision> decisionHandler)
            {
                decisionHandler(WKPermissionDecision.Grant);//Missing this line will result in a blank camera screen
                base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler);
            }
        }
    

    And then set the delegate in OnElementChanged method in your WKWebviewRenderer:

    if (e.NewElement != null)
    {
           UIDelegate = new CustomWebViewDelegate();
    
    }
    

    Reference links:

    Please watch the clip around 12:30 in this Apple's video:Explore WKWebView additions - WWDC21 - Videos - Apple Developer.

    Using Custom Renderers in .NET MAUI

    Use custom renderers in .NET MAUI

    Update:

          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(typeof(HybridWebView), typeof(HybridWebViewHandler));
    
            });
            ;
    
            Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("custom",(handler, view) =>
            {
                handler.PlatformView.UIDelegate = new CustomWebViewDelegate();
            }
            );
    
    
            return builder.Build();
        }
    
    
    #if IOS
        public class CustomWebViewDelegate : WKUIDelegate
    
        {
    
            [Export("webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:")]
    
            public override void RequestMediaCapturePermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, WKMediaCaptureType type, Action<WKPermissionDecision> decisionHandler)
    
            {
                decisionHandler(WKPermissionDecision.Grant);//Missing this line will result in a blank camera screen
    
                base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler);
    
            }
    
        }
    #endif