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

[iOS] Fix crash closing Popup with WebView #21718

Merged
merged 8 commits into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ public MauiWebViewNavigationDelegate(IWebViewHandler handler)
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
var handler = Handler;

if (handler is null || !handler.IsConnected())
return;

var platformView = handler?.PlatformView;
var virtualView = handler?.VirtualView;

if (handler == null || virtualView == null)
if (platformView is null || virtualView is null)
return;

handler.PlatformView?.UpdateCanGoBackForward(handler.VirtualView);
platformView.UpdateCanGoBackForward(virtualView);

if (webView.IsLoading)
return;
Expand All @@ -47,49 +52,67 @@ public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
if (handler is WebViewHandler webViewHandler)
webViewHandler.ProcessNavigatedAsync(url).FireAndForget();
else
handler.PlatformView?.UpdateCanGoBackForward(virtualView);
platformView.UpdateCanGoBackForward(virtualView);
}

[Export("webView:didFailNavigation:withError:")]
public void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
var handler = Handler;

if (handler is null || !handler.IsConnected())
return;

var platformView = handler?.PlatformView;
var virtualView = handler?.VirtualView;

if (handler == null || virtualView == null)
if (platformView is null || virtualView is null)
return;

var url = GetCurrentUrl();

virtualView.Navigated(_lastEvent, url, WebNavigationResult.Failure);

handler.PlatformView?.UpdateCanGoBackForward(virtualView);
platformView.UpdateCanGoBackForward(virtualView);
}

[Export("webView:didFailProvisionalNavigation:withError:")]
public void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
var handler = Handler;

if (handler is null || !handler.IsConnected())
return;

var platformView = handler?.PlatformView;
var virtualView = handler?.VirtualView;

if (handler == null || virtualView == null)
if (platformView is null || virtualView is null)
return;

var url = GetCurrentUrl();

virtualView.Navigated(_lastEvent, url, WebNavigationResult.Failure);

handler.PlatformView?.UpdateCanGoBackForward(virtualView);
platformView.UpdateCanGoBackForward(virtualView);
}

// https://stackoverflow.com/questions/37509990/migrating-from-uiwebview-to-wkwebview
[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
var handler = Handler;

if (handler is null || !handler.IsConnected())
{
decisionHandler.Invoke(WKNavigationActionPolicy.Cancel);
return;
}

var platformView = handler?.PlatformView;
var virtualView = handler?.VirtualView;

if (handler == null || virtualView == null)
if (platformView is null || virtualView is null)
{
decisionHandler.Invoke(WKNavigationActionPolicy.Cancel);
return;
Expand Down Expand Up @@ -130,7 +153,7 @@ public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction,
var lastUrl = request.Url.ToString();

bool cancel = virtualView.Navigating(navEvent, lastUrl);
handler.PlatformView?.UpdateCanGoBackForward(virtualView);
platformView.UpdateCanGoBackForward(virtualView);
decisionHandler(cancel ? WKNavigationActionPolicy.Cancel : WKNavigationActionPolicy.Allow);
}

Expand Down
Loading