Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Catalyst] Allow to use a custom WKUIDelegate on WebView #18483

Closed
wants to merge 10 commits into from
14 changes: 14 additions & 0 deletions src/Controls/samples/Controls.Sample/MauiProgram.cs
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Maui.Foldable;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;

#if COMPATIBILITY_ENABLED
using Microsoft.Maui.Controls.Compatibility.Hosting;
#endif
Expand Down Expand Up @@ -288,6 +289,19 @@ static bool LogEvent(string eventName, string? type = null)
}
});

#if MACCATALYST
Microsoft.Maui.Handlers.WebViewHandler.Mapper.Add(nameof(WebKit.WKUIDelegate), (handler, view) =>
{
if (UIKit.UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
{
MainThread.BeginInvokeOnMainThread(() =>
{
handler.PlatformView.UIDelegate = new Platforms.MacCatalyst.CustomWebViewUIDelegate();
});
}
});
#endif

return appBuilder.Build();
}
}
Expand Down
@@ -0,0 +1,25 @@
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample.Pages.WebViewGalleries
{
public class WebViewGallery : ContentPage
{
public WebViewGallery()
{
Content = new ScrollView
{
Content = new StackLayout
{
Spacing = 5,
Children =
{
GalleryBuilder.NavButton("WebView Playground", () => new WebViewPlayground(), Navigation),
#if MACCATALYST
GalleryBuilder.NavButton("WebView using a custom UIDelegate", () => new WebViewUIDelegatePage(), Navigation),
#endif
}
}
};
}
}
}
@@ -1,7 +1,7 @@
<views:BasePage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.WebViewPage"
x:Class="Maui.Controls.Sample.Pages.WebViewGalleries.WebViewPlayground"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"
Title="WebView">
<views:BasePage.Content>
Expand Down
Expand Up @@ -7,11 +7,11 @@
using Microsoft.Maui.Devices;
using Microsoft.Maui.Storage;

namespace Maui.Controls.Sample.Pages
namespace Maui.Controls.Sample.Pages.WebViewGalleries
{
public partial class WebViewPage
public partial class WebViewPlayground
{
public WebViewPage()
public WebViewPlayground()
{
InitializeComponent();
}
Expand Down
@@ -0,0 +1,15 @@
<views:BasePage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.WebViewGalleries.WebViewUIDelegatePage"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"
Title="WebView">
<views:BasePage.Content>
<AbsoluteLayout>
<WebView
x:Name="DelegateWebView"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="SizeProportional" />
</AbsoluteLayout>
</views:BasePage.Content>
</views:BasePage>
@@ -0,0 +1,14 @@
using System;

namespace Maui.Controls.Sample.Pages.WebViewGalleries
{
public partial class WebViewUIDelegatePage
{
public WebViewUIDelegatePage()
{
InitializeComponent();

DelegateWebView.Source = new Uri("https://webrtc.github.io/samples/src/content/getusermedia/gum/");
}
}
}
@@ -0,0 +1,25 @@
using System;
using Foundation;
using WebKit;


namespace Maui.Controls.Sample.Platforms.MacCatalyst
{
public class CustomWebViewUIDelegate : WKUIDelegate
{
public CustomWebViewUIDelegate()
{

}

[Export("webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:")]
public override void RequestMediaCapturePermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, WKMediaCaptureType type, Action<WKPermissionDecision> decisionHandler)
{
decisionHandler(WKPermissionDecision.Grant);
}
public override void RequestDeviceOrientationAndMotionPermission(WKWebView webView, WKSecurityOrigin origin, WKFrameInfo frame, Action<WKPermissionDecision> decisionHandler)
{
base.RequestDeviceOrientationAndMotionPermission(webView, origin, frame, decisionHandler);
}
}
}
Expand Up @@ -2,6 +2,7 @@
using Maui.Controls.Sample.Models;
using Maui.Controls.Sample.Pages;
using Maui.Controls.Sample.ViewModels.Base;
using Maui.Controls.Sample.Pages.WebViewGalleries;

namespace Maui.Controls.Sample.ViewModels
{
Expand Down Expand Up @@ -87,7 +88,7 @@ public class ControlsViewModel : BaseGalleryViewModel
new SectionModel(typeof(TimePickerPage), "TimePicker",
"A view that allows the user to select a time."),

new SectionModel(typeof(WebViewPage), "WebView",
new SectionModel(typeof(WebViewGallery), "WebView",
"WebView is a view for displaying web and HTML content in your app.")
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Handlers/WebView/WebViewHandler.cs
Expand Up @@ -12,7 +12,7 @@

#if __ANDROID__
using Android.Webkit;
#elif __IOS__
#elif __IOS__ || MACCATALYST
using WebKit;
#endif

Expand All @@ -28,7 +28,7 @@ public partial class WebViewHandler : IWebViewHandler
[nameof(WebViewClient)] = MapWebViewClient,
[nameof(WebChromeClient)] = MapWebChromeClient,
[nameof(WebView.Settings)] = MapWebViewSettings
#elif __IOS__
#elif __IOS__ || MACCATALYST
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this does anything in our code

Semi-confusingly

We already build everything marked as IOS for MACCatalyst
https://github.com/dotnet/maui/blob/main/src/MultiTargeting.targets#L99-L102

I'm not really sure what the users problem is but I don't think this will fix anything for them.

I don't quite understand on the repro, why they are setting the delegate inside a "BeginInvoke" call.

Like, if I check the type of the UIDelegate it's definitely already being replaced

image

you can also see it here that it's already set
image

[nameof(WKUIDelegate)] = MapWKUIDelegate,
#endif
};
Expand Down