Skip to content

Commit

Permalink
fix: Avoid showing error page on cancelled navigation (#1483)
Browse files Browse the repository at this point in the history
This was previously implemented back in the UIWebView days as CB-13222
in #334 and was then proposed
in apache/cordova-plugin-wkwebview-engine#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 <gjudd@stayinfront.com>
  • Loading branch information
dpogue and gwynjudd committed Aug 29, 2024
1 parent b6ae567 commit 8b60dca
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -509,17 +509,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]];
}
}
}

Expand Down

0 comments on commit 8b60dca

Please sign in to comment.