I am implementing a webview based application, in that I need to find out a way when the 404 error occurred.
Anyone's help will be much appreciated.
Thanks to all, Monish.
I am implementing a webview based application, in that I need to find out a way when the 404 error occurred.
Anyone's help will be much appreciated.
Thanks to all, Monish.
In the webViewDidFinishLoad
method, you can also check it this way:
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"status code: %ld", (long)[(NSHTTPURLResponse*)resp.response statusCode]);
webViewDidFinishLoad()
method writes following code and checks status code...
- (void)webViewDidFinishLoad:(UIWebView *)webview {
NSCachedURLResponse *urlResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:webview.request];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) urlResponse.response;
NSInteger statusCode = httpResponse.statusCode;
}
Here you just need to check the status of the request when it finishes or fails in webview delegate method.`
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
int status = [[[webView request] valueForHTTPHeaderField:@"Status"] intValue];
if (status == 404) {
}
}
If this doesn't help you out. Check this one. Create an NSURLRequest with the URL you want to load. Then make the connection using NSURLConnection.
NSURLConnection has a delegate method
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
which will give the the response from the server. Please note that if you are making the connection over HTTP, the response will actually be of class NSHTTPURLResponse. The NSHTTPURLResponse can be used to get the status using the following instance method
- (NSInteger)statusCode
Then check if status = 404 or not and if yes then show your alert view. In this way you will be able to show the html page and the alert view both.
Try this, I think that if URL is wrong then the return html is nil. you can handle it there only
NSString *htmlCode = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:loadUrl]];
if (htmlCode==nil)
{
// you can handle here with an alert or any message in webview to load.
}
else
{
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:loadUrl]]];
}
[htmlCode release];
You can handle using htmlCode in other method like
- (void)webViewDidFinishLoad:(UIWebView *)webView
if You want to handle it after request.