0

Basically, what I want is a function that executes this function [self.navigationController popViewControllerAnimated:YES]; when a certain button is pressed and if you are on a Google page. But the function happens even if I am not on a Google page. Is there something I might be missing?

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    if(wasClicked)
    {
        if([[request.URL absoluteString] rangeOfString:@"google"].location!=NSNotFound)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }

    return YES;
}
Perception
  • 79,279
  • 19
  • 185
  • 195
Moo Moo
  • 85
  • 7

2 Answers2

4

You should check within this method if button is clicked with UIWebViewNavigationTypeFormSubmitted or link is clicked UIWebViewNavigationTypeLinkClicked,

if (navigationType == UIWebViewNavigationTypeLinkClicked) {
}

you should at least read the documentation of the UIWebViewDelegate before posting the question for every single step. I am writing this because in the previous question you asked about how it works, you got answer in other post and you copied the answer and pasted as a question here instead of trying it yourself.

Community
  • 1
  • 1
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
1

If you're on a Google page, there's a strong likelihood that most links will initiate a load request for a URI that contains "google" somewhere in the string. I'm not sure of this, but I think even Google search results first load a Google URL for tracking before redirecting to the actual page clicked.

Try adding NSLog(@"URL: %@", [request.URL absoluteString]); to your function and see why that statement is evaluating to YES.

Josh Hudnall
  • 1,021
  • 8
  • 16