Lots of UIWebViewNavigationTypeBackForward is not only false but makes the actual url impossible

I have a UIWebView and a UITextField for the URL. Naturally, I want the textField to always display the current document url. This works great for urls directly typed into the field, but I also have some buttons attached to the view to reload, back up and fast forward.

So, I added all the UIWebViewDelegate methods to my controller so that it can listen every time the webView moves and changes the url in the textbox as needed.

This is how I use 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 is that the actual click will generate a single "LinkClicked" type navigation followed by a dozen "Other" types (like redirects and ad downloads) which are handled correctly by the code, but the back / forward action generates all of its requests like back / forward requests.

In other words, the click calls setUrlQuietly: once, but the callback / redirect calls it multiple times.

I am trying to use this method to determine if the user actually initiated an action (and I would like to intercept the page redirect too). But if the method is unable to distinguish between the actual "reverse" and "load initiated by the reverse", how can I make this estimate?

Without that, I'm completely obsessed with how I can only show the actual URL and not the intermediate URLs. Thanks!

+2


a source to share


2 answers


Ok, here's what I did - not sure if this is the expected solution. Scratch all the code from the above method and do the following instead:

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

      



This means a little delay between clicking a link, etc. and looking at the url in the textbox, but also ensures that the textField always shows the actual title of the document.

0


a source


I don't see the UIWebViewNavigationTypeBackForward event at all. And sometimes webViewDidStartLoad is never called. There may have been some changes in iOS 5.

Anyway, here's my solution for keeping the title and url up to date. Works very consistently, although the answer might be slightly faster.



- (void)webViewDidFinishLoad:(UIWebView *)wv
{
    [self updateButtons];
    titleLabel.text = [wv stringByEvaluatingJavaScriptFromString:@"document.title"];

    NSURL *url = wv.request.URL;
    [selectedUrl release];
    selectedUrl = [url retain];
}

      

0


a source







All Articles