WKWebView加载URL时随机出现NSURLErrorDomain Code=-999错误求助
Hey there, let’s dig into this tricky random -999 error you’re hitting with WKWebView. First off, this error almost always means a load request got canceled before it finished processing. Since it’s non-deterministic, we’ll need to check a few common scenarios that trigger this:
Concurrent load conflicts
If you’re firing off new load requests (likeloadRequest:orloadHTMLString:) while the WebView is already loading content, the system might cancel the original request to prioritize the new one. Fix this by checking if the WebView is active before starting a new load:if webView.isLoading { webView.stopLoading() } webView.load(yourRequest)Misused navigation delegate callbacks
InwebView(_:decidePolicyFor:decisionHandler:), it’s easy to accidentally call thedecisionHandlermultiple times (e.g., inside async logic) or return.cancelwhen you don’t mean to. Make sure each request only gets one decision, and your logic clearly defines when to allow or cancel navigation:func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { let shouldAllow = // Add your load validation logic here decisionHandler(shouldAllow ? .allow : .cancel) // Never call decisionHandler again after this point! }App lifecycle state disruptions
When your app moves to the background, iOS might pause or cancel in-flight network requests. If you don’t handle state transitions properly (like resuming loads when returning to the foreground), you could get this error. Try adding lifecycle hooks:func applicationWillResignActive(_ application: UIApplication) { webView?.stopLoading() } func applicationDidBecomeActive(_ application: UIApplication) { if let currentURL = webView?.url, !webView.isLoading { webView?.load(URLRequest(url: currentURL)) } }Reused WKWebView instances with leftover state
If you’re reusing the same WKWebView for multiple tasks, old pending requests might clash with new ones. Either create a fresh instance for each load task, or make sure to clear all previous state (stop loading, reset cookies/storage if needed) before reusing.Race conditions with network interceptors
If you’re using third-party network libraries or custom URL protocols alongside WKWebView, they might interfere with the WebView’s native network stack. Try temporarily disabling these interceptors to see if the error goes away—this can help isolate the conflict.
For debugging, add detailed logging in your failure delegate to capture more context when the error hits:
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { if let nsError = error as NSError?, nsError.code == -999 { print("Canceled load details: \(nsError.userInfo)") // Optional: Retry the load after a short delay (avoid infinite loops!) DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { if !webView.isLoading { webView.reload() } } } }
内容的提问来源于stack exchange,提问作者rajat yadav

