如何修复 MAUI iOS Web 视图中的双重权限弹出窗口。

huangapple go评论74阅读模式
英文:

How to fix double permission popup in MAUI ios web view

问题

我有一个Maui应用,尝试在WebView内打开我的流媒体网站。

但每次我在Maui WebView内打开我的流媒体网站时,网站都会要求摄像头权限,这对用户来说很烦人(即使用户在第一次安装应用时已经授予了摄像头访问权限)。

我的问题是,是否有办法禁用WebView网站内的权限提示,让网站使用应用程序自身的授予的权限。

是否有人遇到类似的问题?

感谢您的帮助。

英文:

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.

答案1

得分: 0

using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;

...

var webView = new WebView
{
    Source = "https://example.com"
};

webView.On<iOS>().SetAllowsInlineMediaPlayback(true);
webView.On<iOS>().SetPrefersEphemeralWebBrowserSession(true);

// only request permissions when the user interacts with the web view
webView.On<iOS>().SetShouldPromptForPermission(false);

Content = webView;
英文:
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;

...

var webView = new WebView
{
    Source = &quot;https://example.com&quot;
};

webView.On&lt;iOS&gt;().SetAllowsInlineMediaPlayback(true);
webView.On&lt;iOS&gt;().SetPrefersEphemeralWebBrowserSession(true);

// only request permissions when the user interacts with the web view
webView.On&lt;iOS&gt;().SetShouldPromptForPermission(false);

Content = webView;

答案2

得分: 0

使用getUserMedia()获取相机。您可以在iOS的WKWebView中为WKUIDelegate设置委托。

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); // 如果缺少这一行,会导致相机屏幕为空白
        base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler);
    }
}

然后,在WKWebviewRendererOnElementChanged方法中设置委托:

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

参考链接:

请在苹果的视频中约12:30的位置观看此视频片段:Explore WKWebView additions - WWDC21 - Videos - Apple Developer

在.NET MAUI中使用自定义渲染器

在.NET MAUI中使用自定义渲染器

更新:

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); // 如果缺少这一行,会导致相机屏幕为空白
        base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler);
    }
}
#endif
英文:

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

public class CustomWebViewDelegate :WKUIDelegate
       {
         [Export(&quot;webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:&quot;)]
        public override void RequestMediaCapturePermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, WKMediaCaptureType type,Action&lt;WKPermissionDecision&gt; 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&lt;App&gt;()
                  .ConfigureFonts(fonts =&gt;
                  {
                        fonts.AddFont(&quot;OpenSans-Regular.ttf&quot;, &quot;OpenSansRegular&quot;);
                        fonts.AddFont(&quot;OpenSans-Semibold.ttf&quot;, &quot;OpenSansSemibold&quot;);
                  })

        .ConfigureMauiHandlers(handlers =&gt;

        {

            handlers.AddHandler(typeof(HybridWebView), typeof(HybridWebViewHandler));

        });
        ;

        Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping(&quot;custom&quot;,(handler, view) =&gt;
        {
            handler.PlatformView.UIDelegate = new CustomWebViewDelegate();
        }
        );


        return builder.Build();
    }


#if IOS
    public class CustomWebViewDelegate : WKUIDelegate

    {

        [Export(&quot;webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:&quot;)]

        public override void RequestMediaCapturePermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, WKMediaCaptureType type, Action&lt;WKPermissionDecision&gt; decisionHandler)

        {
            decisionHandler(WKPermissionDecision.Grant);//Missing this line will result in a blank camera screen

            base.RequestMediaCapturePermission(webView, origin, frame, type, decisionHandler);

        }

    }
#endif

答案3

得分: 0

以下是代码的翻译部分:

WKWebViewConfiguration config = MauiWKWebView.CreateConfiguration();
config.ApplicationNameForUserAgent = "Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0";
config.AllowsInlineMediaPlayback = true;
config.MediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypes.None;
WebViewHandler.PlatformViewFactory = handler => new MauiWKWebView(CGRect.Empty, (WebViewHandler)handler, config)
{
  UIDelegate = new CustomWebViewDelegate(),
};

请注意,代码中的“RequestMediaCapturePermission is not called at all. Any help is greatly appreciated.” 部分没有被翻译,因为它是关于代码功能的附加信息。如果您需要关于这部分的翻译或有其他问题,请随时提问。

英文:
WKWebViewConfiguration config = MauiWKWebView.CreateConfiguration();
config.ApplicationNameForUserAgent = &quot;Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0&quot;;`
config.AllowsInlineMediaPlayback = true;
config.MediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypes.None;
WebViewHandler.PlatformViewFactory = handler =&gt; new MauiWKWebView(CGRect.Empty, (WebViewHandler)handler, config)
{
  UIDelegate = new CustomWebViewDelegate(),
};

The CustomWebViewDelegate is exactly the same as above. However, RequestMediaCapturePermission is not called at all. Any help is greatly appreciated.

huangapple
  • 本文由 发表于 2023年3月4日 04:50:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定