0

I am loading a webpage using UIWebView in my iOS 7 application. I have a few broken links in my webpage.

When I click on these links, I want to intimate the user with an alertView. I implemented the following delegates but no response was obtained.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;

Note: These delegate methods get called when I click on non-broken links.

So is there any way by which the webView can respond to broken links too?

Thanks in advance.

Javier Enciso
  • 55
  • 1
  • 2
  • 11
Benny Dalby
  • 182
  • 4

2 Answers2

0

Try this:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    int status = [[[webView request] valueForHTTPHeaderField:@"Status"] intValue];
    if (status == 404) {

    }
}

From here you can check status. Hope this helps .. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
  • Yes you are right.But this delegate does not respond when i click on broken links.Thats my issue.If it was called,then yes, we can check the current status and perform the task accordingly.But i am hit with the constraint of UIWebView not responding when such links are clicked – Benny Dalby Apr 07 '14 at 09:21
0

A possibility is to consume shouldStartLoadWithRequest.

You should:

  1. Get the url from request.
  2. Send an asynchronous request to the url.
  3. Check if you get response. If not response cancel this request otherwise allow it by returning TRUE.

Have a look at how you can send request.

Checking reachability again a specific page

Is it wise to use Reachability to check for a remote host's availability

Using this way you can cancel request and inform user that the specified page is not available. You can also check for HTTP Status Code of response which is usually 200, if you have a valid response.

Hope it helps!

Another way is to handle it from your webpage itself, i.e., redirect it to some common page that says the page is unavailable and a back to redirect it to the previous page.

EDIT:

Try this code in separate project h file:

@interface ViewController : UIViewController<UIWebViewDelegate>

and in m file:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

     NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];

     UIWebView *wbWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self.view addSubview:wbWebView];
    [wbWebView setDelegate:self];   // <------------- set delegate
    [wbWebView loadRequest:request];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"request loading... %@", [request URL].absoluteString);

    // TODO : Validate here
    // If validated then return TRUE otherwise false;
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"request finished loading");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"request failed loading");
}
Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • shouldStartLoadWithRequest: delegate method is not invoked when i click on such links.Only for normal links will this delegate gets called.No response is obtained from the UIWebView delegates. – Benny Dalby Apr 07 '14 at 08:59