How to fix double permission popup in xamarin ios web view

Anila Mathew 25 Reputation points
2023-02-06T09:19:10.4933333+00:00

When I am trying to use camera in a web view, there are two permissions that are asked in iOS. One is the popup created from browser and the other is created from the Phone. Are there are any ways to avoid the browser popup for camera permission?

Update

The webpage is using getUserMedia() and so the popup is coming in ios Safari browser. The code I have used in WKWebviewRenderer is :

const string JavaScriptFunction2 = "window.navigator.getUserMedia = function() {webkit.messageHandlers.callbackHandler.postMessage(arguments);};";
var script2 = new WKUserScript(new NSString(JavaScriptFunction2), WKUserScriptInjectionTime.AtDocumentStart, false);
 userController.AddUserScript(script2);
 userController.AddScriptMessageHandler(this, "callbackHandler");

And in DidReceiveScriptMessage function :

 if (message.Name == "callbackHandler")
 {
                   ((HybridWebView)Element).InvokeAction(message.Body.ToString());
  }

The DidReceiveScriptMessage is not getting called..

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,272 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 25,341 Reputation points Microsoft Vendor
    2023-02-09T06:56:40.5266667+00:00

    Hello,
    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);//As you (Anila Mathew) said, missing this line will result in a blank camera screen
                base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler);
            }
        }
    

    Set the delegate in OnElementChanged method in your WKWebviewRenderer

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

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

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentationto enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Anila Mathew 25 Reputation points
    2023-02-09T06:49:25.3433333+00:00

    The answer which helped me to solve this :

        public class CustomWebViewDelegate : WKUIDelegate
        {
            [Export("webView:decideMediaCapturePermissionsForOrigin:initiatedByFrame:type:decisionHandler:")]
            public override void RequestMediaCapturePermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, WKMediaCaptureType type, Action<WKPermissionDecision> decisionHandler)
            {
                try
                {
                    decisionHandler(WKPermissionDecision.Grant);
                   base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler);
                }
                catch (Exception e)
                { }
            }
        }
    
    

    And call this function in webview:

    webView.UIDelegate = new CustomWebViewDelegate();

    1 person found this answer helpful.

  2. Learnaholic Pte Ltd 0 Reputation points
    2023-09-22T09:25:05.9866667+00:00

    I faced the same issue and followed the suggested answer. But the RequestMediaCapturePermission is not called at all.

    I raised an issue in Github. (https://github.com/dotnet/maui/issues/17431)

    Any help is very much appreciated.