How to detect and handle HTTP error codes in UIWebView?

前端 未结 9 714
无人及你
无人及你 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:38

    In webViewDidFinishLoad:

    if ([[(NSHTTPURLResponse*)[[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request] valueForHTTPHeaderField:@"Status"] intValue] == 404){
    }
    

    You may consider this solution over other more complex ones, even though some responses might not get cached. Note that wrong urls are usually getting cached by a system which has default configurations.

提交回复
热议问题