iosmauiwkwebview

RequestCaptureMediaPermission for WKWebView in Maui iOS


In Microsoft Learn (https://learn.microsoft.com/en-us/answers/questions/1167987/how-to-fix-double-permission-popup-in-xamarin-ios) a solution is given to reduce the number of prompts for camera and microphone permission when WebRTC is used in Xamarin iOS. I could not get it working on Maui IOS. I have also opened a ticket in GitHub (https://github.com/dotnet/maui/issues/17431)

The API RequestMediaCapturePermission is not called at all. I am not sure where is the error in my code in the small project that I have uploaded in GitHub. Greatly appreciate any help, especially from those who managed to get it working.

Thank you.


Solution

  • From the docs: https://github.com/dotnet/maui/blob/main/src/Core/src/Handlers/WebView/WebViewHandler.cs#L32, the IWebViewHandler on iOS has been defined like below:

    #elif __IOS__
                [nameof(WKUIDelegate)] = MapWKUIDelegate,
    #endif
    

    To call the RequestMediaCapturePermission, you may change the "PermissionRequest" to nameof(WKUIDelegate):

    
    #if IOS || MACCATALYST
            Microsoft.Maui.Handlers.WebViewHandler.Mapper.Add(nameof(WKUIDelegate), (handler, view) =>
                {
                    if (UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
                    {
                      MainThread.BeginInvokeOnMainThread(() =>
                      {
                        handler.PlatformView.UIDelegate = new WebViewUIDelegate();
                      });
                    }
                });
            return builder.Build();
    #endif
    
      [Export("webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:")]
      public override void RequestMediaCapturePermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, WKMediaCaptureType type, Action<WKPermissionDecision> decisionHandler)
     {
          decisionHandler(WKPermissionDecision.Grant);
          base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler );
      }