From 82933a8c0848ad88b5b8b4e3fd6c13ab42eda126 Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Wed, 28 Aug 2024 01:19:34 -0700 Subject: [PATCH] fix: Avoid showing error page on cancelled navigation This was previously implemented back in the UIWebView days as CB-13222 in https://github.com/apache/cordova-ios/pull/334 and was then proposed in https://github.com/apache/cordova-plugin-wkwebview-engine/pull/84 for the WKWebView plugin, but that didn't get reviewed/merged before the repo was archived. The fix is still valid, so we'll port it to the current version of the WKWebView plugin. Closes GH-959. Co-Authored-By: Gwyn Judd --- .../Plugins/CDVWebViewEngine/CDVWebViewEngine.m | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m index c47cd8556..6e57c0f7d 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m @@ -511,17 +511,17 @@ - (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(WKNavigatio - (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigation withError:(NSError*)error { - CDVViewController* vc = (CDVViewController*)self.viewController; - NSString* message = [NSString stringWithFormat:@"Failed to load webpage with error: %@", [error localizedDescription]]; NSLog(@"%@", message); - NSURL* errorUrl = vc.errorURL; - if (errorUrl) { - NSCharacterSet *charSet = [NSCharacterSet URLFragmentAllowedCharacterSet]; - errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [message stringByAddingPercentEncodingWithAllowedCharacters:charSet]] relativeToURL:errorUrl]; - NSLog(@"%@", [errorUrl absoluteString]); - [theWebView loadRequest:[NSURLRequest requestWithURL:errorUrl]]; + if (error.code != NSURLErrorCancelled) { + NSURL* errorUrl = self.viewController.errorURL; + if (errorUrl) { + NSCharacterSet *charSet = [NSCharacterSet URLFragmentAllowedCharacterSet]; + errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [message stringByAddingPercentEncodingWithAllowedCharacters:charSet]] relativeToURL:errorUrl]; + NSLog(@"%@", [errorUrl absoluteString]); + [theWebView loadRequest:[NSURLRequest requestWithURL:errorUrl]]; + } } }