6

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.

Monish Kumar
  • 2,788
  • 4
  • 38
  • 54

4 Answers4

1

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]);
Sandy D.
  • 3,166
  • 1
  • 20
  • 31
1

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;
}
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
Subhash Khimani
  • 427
  • 7
  • 22
-1

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.

Aisha
  • 1,559
  • 5
  • 20
  • 37
  • @Shruthi:But when the server is failed the webViewDidFinishLoad method is not calling. – Monish Kumar Dec 21 '10 at 12:22
  • 1
    Then try this in webView's didFailLoadWithError delegate method. – Aisha Dec 21 '10 at 12:25
  • @shruthi:Sorry webViewDidFinishLoad this method is called but the status I got is 0 instead of 404. – Monish Kumar Dec 21 '10 at 12:30
  • first of all its shruti not shruthi and secondly if the status you are getting is 0 then its not a 404 error for which you are searching for. If it had been 404 error then on displaying the url in browser your page should display something like "404 Error while displaying the page". In your case you can check if the status==0 then do whatever you wanna do in the if part. Hope this works with you. – Aisha Dec 21 '10 at 12:38
  • Hey KK Shruti... Now its Ok na :)?? K I will go for your suggestion and check it once again.Thanks for ur suggestion. – Monish Kumar Dec 21 '10 at 12:43
  • @Shruti:when I paste the url in browser it showing as"HTTP - 404 Page Not Found Sorry, the page you were looking for cannot be found." – Monish Kumar Dec 21 '10 at 12:46
  • Strange then the status must return 404 only. Can you send me the url plz. – Aisha Dec 21 '10 at 12:51
  • @Shruti:Actually the url u asked is confidential I cannot post it here.I had known the problem why the 404 error is not generated.They are handled it at their server side if 404 error generated they giving the return string as html file,so 404 is not generated for me. – Monish Kumar Dec 22 '10 at 07:52
  • Thanks for ur suggessions n all..!! – Monish Kumar Dec 22 '10 at 07:53
  • we need to show both the html content in webview and to show an alert as server not responding.I donno how can I reach this. – Monish Kumar Dec 22 '10 at 07:59
  • @Monish I resolved your problem. Please see the edited answer. – Aisha Dec 22 '10 at 10:20
  • @shruti:But it makes nother call to the Server. – Monish Kumar Dec 22 '10 at 10:31
  • @Monish: I din't get it what you said. I tried this in my project and it worked successfully. – Aisha Dec 22 '10 at 10:35
  • Yeah it works fine.But we use request connection using that url.we are again making call to the server.I dont want to call again the server.. – Monish Kumar Dec 22 '10 at 10:37
  • @Monish: I worked hard on your problem and this is the only solution I can provide you and I suppose this is the only solution to it. What if it makes another call to the server. If yoy have to load the request in webview then this can be the only approach I think. – Aisha Dec 22 '10 at 10:54
  • @Monish: please delete the last two comments of yours. – Aisha Dec 23 '10 at 06:47
  • 7
    This approach does not work. There is no way to access the *HTTP response* status coden from a webView which is what you need. See http://stackoverflow.com/questions/14451012/uiwebview-not-go-to-didfailloadwitherror-when-weblink-not-found – phatblat Dec 17 '13 at 20:17
-4

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.

Madan Mohan
  • 8,764
  • 17
  • 62
  • 96