WKWebView catch HTTP error codes with Swift 4

Using a WKNavigationDelegate on the WKWebView you can get the status code from the response each time one is received.

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse,
             decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

    if let response = navigationResponse.response as? HTTPURLResponse {
        if response.statusCode == 401 {
            // ...
        }
    }
    decisionHandler(.allow)
}