How to detect and handle HTTP error codes in UIWebView?

前端 未结 9 705
无人及你
无人及你 2021-02-02 11:54

I want to inform user when HTTP error 404 etc is received. How can I detect that? I\'ve already tried to implement

- (void)webView:(UIWebView *)webView didFailL         


        
9条回答
  •  悲哀的现实
    2021-02-02 12:31

    Heres is my swift 3 version of @AxelGuilmin response:

    func webViewDidFinishLoad(_ webView: UIWebView) {
    
            guard let request = webView.request else { return }
    
            let cachedUrlResponse = URLCache.shared.cachedResponse(for: request)
            let httpUrlResponse = cachedUrlResponse?.response as? HTTPURLResponse
            if let statusCode = httpUrlResponse?.statusCode {
                if statusCode == 404 {
                    // Handling 404 response
                }
            }
        }
    

提交回复
热议问题