From bfc0309a137f48fe915fd10f7def364e02899a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Mon, 15 Apr 2024 18:44:25 +0200 Subject: [PATCH 1/5] [iOS] Fix crash closing Popup with WebView (#21718) * Added repro sample * Fix the issue * Added UI Test * Updated csproj * More changes * Removed sample and test * More changes * Removed unnecesary changes --- .../iOS/MauiWebViewNavigationDelegate.cs | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs b/src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs index 6f1a916c218b..3000e3ec430d 100644 --- a/src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs +++ b/src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs @@ -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; @@ -47,39 +52,49 @@ 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 @@ -87,9 +102,17 @@ public void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigat public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action 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; @@ -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); } From cf32698e65cc6df3e969fe34db2a53dd242ebd5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Thu, 18 Apr 2024 09:38:06 +0200 Subject: [PATCH 2/5] Added UITest --- .../Issues/Issue21846.xaml | 17 ++++++++++ .../Issues/Issue21846.xaml.cs | 22 ++++++++++++ .../Issues/Issue21846Modal.xaml | 20 +++++++++++ .../Issues/Issue21846Modal.xaml.cs | 34 +++++++++++++++++++ .../tests/UITests/Tests/Issues/Issue21846.cs | 28 +++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 src/Controls/samples/Controls.Sample.UITests/Issues/Issue21846.xaml create mode 100644 src/Controls/samples/Controls.Sample.UITests/Issues/Issue21846.xaml.cs create mode 100644 src/Controls/samples/Controls.Sample.UITests/Issues/Issue21846Modal.xaml create mode 100644 src/Controls/samples/Controls.Sample.UITests/Issues/Issue21846Modal.xaml.cs create mode 100644 src/Controls/tests/UITests/Tests/Issues/Issue21846.cs diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21846.xaml b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21846.xaml new file mode 100644 index 000000000000..97b83ee4f6fa --- /dev/null +++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21846.xaml @@ -0,0 +1,17 @@ + + + + +