Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get failed URL from didFailProvisionalNavigation method

I'm trying to display error message for my web view, and I need to know url which is not available, so I implemented delegate method:

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [self.addressBar finishLoadingProgressAnimated:YES];
    NSLog(@"%@", webView.URL);
    [self showErrorPageForURL:error.userInfo[NSErrorFailingURLStringKey]];
}

but NSErrorFailingURLStringKey is deprecated, so how can I get failed URL? WKNavigation's interface is empty. webView.URL == nil at that moment.

like image 507
BergP Avatar asked Mar 17 '23 18:03

BergP


2 Answers

Swift 3 or Swift 4

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    if error._domain == "WebKitErrorDomain" {
        if let info = error._userInfo as? [String: Any] {
            if let url = info["NSErrorFailingURLKey"] as? URL {

            }
            if let urlString = info["NSErrorFailingURLStringKey"] as? String {

            }
        }
    }
}
like image 109
Shingo Fukuyama Avatar answered Apr 26 '23 07:04

Shingo Fukuyama


You can use NSURLErrorFailingURLStringErrorKey to replace NSErrorFailingURLStringKey. If you jump to its definition in Xcode, you will find the below discussion.

This constant supersedes NSErrorFailingURLStringKey, which was deprecated in Mac OS X 10.6. Both constants refer to the same value for backward-compatibility, but this symbol name has a better prefix.

like image 20
gabbler Avatar answered Apr 26 '23 09:04

gabbler