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.
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 {
}
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With