Skip to content

Commit

Permalink
[iOS] Fix crash closing Popup with WebView (#21718)
Browse files Browse the repository at this point in the history
* Added repro sample

* Fix the issue

* Added UI Test

* Updated csproj

* More changes

* Removed sample and test

* More changes

* Removed unnecesary changes
  • Loading branch information
jsuarezruiz authored Apr 15, 2024
1 parent 86b5635 commit 7f85d2a
Showing 1 changed file with 32 additions and 9 deletions.
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

0 comments on commit 7f85d2a

Please sign in to comment.