使用WKWebView加载网站触发didFailNavigation回调,报错NSURLErrorDomain Code=-999
WKWebView 错误码 -999 (NSURLErrorDomain) 解决方案
Hey there, let's break down this -999 error you're hitting with WKWebView. I've tackled this issue more times than I can count, so here's the lowdown:
First off, that -999 error in the NSURLErrorDomain almost always means a request was canceled. The most common scenarios are:
- A new network request kicks off before an existing one finishes, so WKWebView cancels the old one to prioritize the new (like if a user taps links quickly in succession)
- You explicitly called
webView.stopLoading()somewhere in your code during navigation - Internal WKWebView resource loading conflicts that cause a pending request to get axed
How to handle it:
- Ignore harmless cancellations: Most of the time, this error isn't actually a "failure"—it's just WKWebView cleaning up old requests. You can add a check in your delegate method to skip handling it:
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { guard let nsError = error as NSError? else { return } // Skip -999 cancellation errors if nsError.code == -999 && nsError.domain == NSURLErrorDomain { return } // Deal with actual loading failures here print("Genuine load error: \(error.localizedDescription)") } - Check for intentional stops: Double-check your code to see if you're calling
stopLoading()at any point during navigation. If you are, this error is expected behavior. - Validate your URL setup: Even though the error shows empty URL fields, make sure the URL you're passing to
load(_:)orloadRequest(_:)is valid (no empty strings, malformed schemes, etc.). A bad initial URL can trigger an immediate cancellation. - Test with default configuration: If you're using a custom
WKWebViewConfiguration, try switching to the default setup temporarily. Sometimes custom process pools or user content controllers can cause unexpected request cancellations.
In most cases, you don't need to stress about this error—it's just part of WKWebView's normal request management. Only dig deeper if it's actually breaking your app's functionality.
内容的提问来源于stack exchange,提问作者stspb
相关产品推荐
相关产品推荐

