现在的位置: 首页 > 综合 > 正文

UIWebView如何判断 HTTP 404 等错误

2017年10月04日 ⁄ 综合 ⁄ 共 961字 ⁄ 字号 评论关闭

先用

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];  

判断一下当前的链接。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

   
if
((([httpResponse statusCode]/100) ==
2)) {

       
// self.earthquakeData = [NSMutableData data];

        [UIApplication sharedApplication].networkActivityIndicatorVisible =
YES;

        

        [ webView loadRequest:[ NSURLRequest requestWithURL: myUrl ]];

        webView.delegate =
self;

    }
else
{

        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:

                                 
NSLocalizedString(@"HTTP Error",

                                                   
@"Error message displayed when receving a connection error.")

                                                             forKey:NSLocalizedDescriptionKey];

       
NSError
*error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];

        

       
if
([error code] == 404) {

            NSLog(@"xx");

            webView.hidden =
YES;

        }        

        

    }

}

抱歉!评论已关闭.