views:

16

answers:

1

I have a UIWebView and a UITextField for the url. Naturally, I want the textField to always show the current document url. This works fine for urls directly input in the field, but I also have some buttons attached to the view for reload, back, and forward.

So I've added all the UIWebViewDelegate methods to my controller, so it can listen to whenever the webView navigates and change the url in the textField as needed.

Here's how I'm using the shouldStartLoadWithRequest: method:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
 NSLog(@"navigated via %d", navigationType);
 //loads the user cares about
 if ( navigationType == UIWebViewNavigationTypeLinkClicked 
  || navigationType == UIWebViewNavigationTypeBackForward ) {
  //URL setting
  [self setUrlQuietly:request.URL];

 }

    return YES;
}

Now, my problem here is that an actual click will generate a single navigation of type "LinkClicked" followed by a dozen type "Other" (redirects and ad loads I assume), which gets handled correctly by the code, but a back/forward action will generate all its requests as back/forward requests.

In other words, a click calls setUrlQuietly: once, but a back/forward calls it multiple times.

I am trying to use this method to determine if the user actually initiated the action (and I'd like to catch page redirects too). But if the method has no way of distinguishing between an actual "back" and a "load initiated as a result of a back", how can I make this assessment?

Without this, I am completely stumped as to how I can only show the actual url and not intermediate urls. Thank you!

A: 

Ok here's what I ended up doing - not sure if this is the expected solution though. Scratch all the code from the above method and instead do this:

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSString *urlString = [self.webView stringByEvaluatingJavaScriptFromString:@"document.URL"]
    [self setUrlQuietly:[NSURL URLWithString:urlString]];
}

This means there's a slight delay between clicking a link etc and seeing the url show up in the textField, but it also guarantees that the textField always shows the document's actual title.

SG1