UIWebView如何判断 HTTP 404 等错误

先用

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;

        }        

        

    }

}


你可能感兴趣的:(UIWebView如何判断 HTTP 404 等错误)