英文:
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 = "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;
答案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);
}
}
然后,在WKWebviewRenderer
的OnElementChanged
方法中设置委托:
if (e.NewElement != null)
{
UIDelegate = new CustomWebViewDelegate();
}
参考链接:
请在苹果的视频中约12:30的位置观看此视频片段:Explore WKWebView additions - WWDC21 - Videos - Apple Developer。
更新:
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("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
答案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 = "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(),
};
The CustomWebViewDelegate is exactly the same as above. However, RequestMediaCapturePermission is not called at all. Any help is greatly appreciated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论