Moo Moo
Moo Moo

Reputation: 85

Bool web view function not working as planned

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;
}

Upvotes: 0

Views: 152

Answers (2)

nsgulliver
nsgulliver

Reputation: 12671

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.

Upvotes: 4

Josh Hudnall
Josh Hudnall

Reputation: 1031

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.

Upvotes: 1

Related Questions