/
MauiWKWebView.cs
211 lines (181 loc) · 6.93 KB
/
MauiWKWebView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using CoreGraphics;
using Foundation;
using Microsoft.Extensions.Logging;
using UIKit;
using WebKit;
namespace Microsoft.Maui.Platform
{
public class MauiWKWebView : WKWebView, IWebViewDelegate, IUIViewLifeCycleEvents
{
[UnconditionalSuppressMessage("Memory", "MA0002", Justification = "Used to persist cookies across WebView instances. Not a leak.")]
static WKProcessPool? SharedPool;
string? _pendingUrl;
readonly WeakReference<WebViewHandler> _handler;
public MauiWKWebView(WebViewHandler handler)
: this(RectangleF.Empty, handler)
{
}
public MauiWKWebView(CGRect frame, WebViewHandler handler)
: this(frame, handler, CreateConfiguration())
{
}
public MauiWKWebView(CGRect frame, WebViewHandler handler, WKWebViewConfiguration configuration)
: base(frame, configuration)
{
_ = handler ?? throw new ArgumentNullException("handler");
_handler = new WeakReference<WebViewHandler>(handler);
BackgroundColor = UIColor.Clear;
AutosizesSubviews = true;
NavigationDelegate = new MauiWebViewNavigationDelegate(handler);
}
public string? CurrentUrl =>
Url?.AbsoluteUrl?.ToString();
public override void MovedToWindow()
{
base.MovedToWindow();
if (!string.IsNullOrWhiteSpace(_pendingUrl))
{
var closure = _pendingUrl;
_pendingUrl = null;
// I realize this looks like the worst hack ever but iOS 11 and cookies are super quirky
// and this is the only way I could figure out how to get iOS 11 to inject a cookie
// the first time a WkWebView is used in your app. This only has to run the first time a WkWebView is used
// anywhere in the application. All subsequents uses of WkWebView won't hit this hack
// Even if it's a WkWebView on a new page.
// read through this thread https://developer.apple.com/forums/thread/99674
// Or Bing "WkWebView and Cookies" to see the myriad of hacks that exist
// Most of them all came down to different variations of synching the cookies before or after the
// WebView is added to the controller. This is the only one I was able to make work
// I think if we could delay adding the WebView to the Controller until after ViewWillAppear fires that might also work
// But we're not really setup for that
// If you'd like to try your hand at cleaning this up then UI Test Issue12134 and Issue3262 are your final bosses
InvokeOnMainThread(async () =>
{
await Task.Delay(500);
if (_handler.TryGetTarget(out var handler))
await handler.FirstLoadUrlAsync(closure);
});
}
_movedToWindow?.Invoke(this, EventArgs.Empty);
}
[Export("webView:didFinishNavigation:")]
public async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
var url = CurrentUrl;
if (url == null || url == $"file://{NSBundle.MainBundle.BundlePath}/")
return;
if (_handler.TryGetTarget(out var handler))
await handler.ProcessNavigatedAsync(url);
}
public void LoadHtml(string? html, string? baseUrl)
{
if (html != null)
LoadHtmlString(html, baseUrl == null ? new NSUrl(NSBundle.MainBundle.BundlePath, true) : new NSUrl(baseUrl, true));
}
async Task LoadUrlAsync(string? url)
{
try
{
var uri = new Uri(url ?? string.Empty);
var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute);
var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}", UriKind.Relative);
var safeFullUri = new Uri(safeHostUri, safeRelativeUri);
NSUrlRequest request = new NSUrlRequest(new NSUrl(safeFullUri.AbsoluteUri));
if (_handler.TryGetTarget(out var handler))
{
if (handler.HasCookiesToLoad(safeFullUri.AbsoluteUri) &&
!(OperatingSystem.IsIOSVersionAtLeast(11) || OperatingSystem.IsTvOSVersionAtLeast(11)))
{
return;
}
await handler.SyncPlatformCookiesAsync(safeFullUri.AbsoluteUri);
}
LoadRequest(request);
}
catch (UriFormatException formatException)
{
// If we got a format exception trying to parse the URI, it might be because
// someone is passing in a local bundled file page. If we can find a better way
// to detect that scenario, we should use it; until then, we'll fall back to
// local file loading here and see if that works:
if (!string.IsNullOrEmpty(url))
{
if (!LoadFile(url))
{
if (_handler.TryGetTarget(out var handler))
handler.MauiContext?.CreateLogger<MauiWKWebView>()?.LogWarning($"Unable to Load Url {url}: {formatException}");
}
}
}
catch (Exception exc)
{
if (_handler.TryGetTarget(out var handler))
handler.MauiContext?.CreateLogger<MauiWKWebView>()?.LogWarning($"Unable to Load Url {url}: {exc}");
}
}
public void LoadUrl(string? url)
{
LoadUrlAsync(url).FireAndForget();
}
// https://developer.apple.com/forums/thread/99674
// WKWebView and making sure cookies synchronize is really quirky
// The main workaround I've found for ensuring that cookies synchronize
// is to share the Process Pool between all WkWebView instances.
// It also has to be shared at the point you call init
public static WKWebViewConfiguration CreateConfiguration()
{
// By default, setting inline media playback to allowed, including autoplay
// and picture in picture, since these things MUST be set during the webview
// creation, and have no effect if set afterwards.
// A custom handler factory delegate could be set to disable these defaults
// but if we do not set them here, they cannot be changed once the
// handler's platform view is created, so erring on the side of wanting this
// capability by default.
var config = new WKWebViewConfiguration();
if (OperatingSystem.IsMacCatalystVersionAtLeast(10) || OperatingSystem.IsIOSVersionAtLeast(10))
{
config.AllowsPictureInPictureMediaPlayback = true;
config.AllowsInlineMediaPlayback = true;
config.MediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypes.None;
}
if (SharedPool == null)
SharedPool = config.ProcessPool;
else
config.ProcessPool = SharedPool;
return config;
}
bool LoadFile(string url)
{
try
{
var file = Path.GetFileNameWithoutExtension(url);
var ext = Path.GetExtension(url);
var nsUrl = NSBundle.MainBundle.GetUrlForResource(file, ext);
if (nsUrl == null)
{
return false;
}
LoadFileUrl(nsUrl, nsUrl);
return true;
}
catch (Exception ex)
{
if (_handler.TryGetTarget(out var handler))
handler.MauiContext?.CreateLogger<MauiWKWebView>()?.LogWarning($"Could not load {url} as local file: {ex}");
}
return false;
}
[UnconditionalSuppressMessage("Memory", "MA0002", Justification = IUIViewLifeCycleEvents.UnconditionalSuppressMessage)]
EventHandler? _movedToWindow;
event EventHandler IUIViewLifeCycleEvents.MovedToWindow
{
add => _movedToWindow += value;
remove => _movedToWindow -= value;
}
}
}