views:

1067

answers:

4

In my iPhone app, I have a UIWebView where I'm loading a local html. Everything works up just fine, but now I want to be able to handle local links (to footnotes): when I click on a local link, I want to be able to jump to the footnote refered by it, then be able to come back.

<a href="#tofootnote">jump to footnote</a>

I handled this by adding my code to shouldStartLoadWithRequest and intercepting local link clicks; when a local link is clicked, I work my magic (hide some ui elements, add a back button, etc..); clicking on the back button jumps me back to the original location in the html document.

The problem is that clicking ONCE AGAIN on the link no longer calls shouldStartLoadWithRequest. I.e., the following code:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
 NSLog(@"foo bar");
 return YES;
}

only displays "foo bar" ONCE (actually, twice - the first time when I'm initially loading the document), but only ONCE afterwards, regardless of how many times I'm clicking on the local link

So, unless anyone has a better explanation, I'm guessing that the UIWebView caches the doc or link or something and no longer calls the handler after the initial call; If this is the case, how can I clear this internal cache up? (without reloading the document)

A: 

Try 'touch'ing the anchor tag (assign it to itself). My hypothesis is that will cause the DOM to update, making it check the link again. Haven't tested this, it's just an idea!

SilverSun
I'm having the exact same problem at the moment.. I tried your solution, and it doesn't help.
GrafikRobot
A: 

This seems to be happening for me too when I use an onclick="window.location.hash = ...; return false;" instead of capturing the click.

If I scroll around a little bit or click on another anchor, I can use the link again.

Paul Shapiro
A: 

NSURLRequest* request = [NSURLRequest requestWithURL:fileURL cachePolocy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];

[webView loadRequest:request];

Biranchi
I'm having the exact same problem at the moment.. And I attest that changing the cache policy option doesn't work.
GrafikRobot
+1  A: 

The only solution I've found for this problem is to string-replace all the anchors from "#anchor" to something that is considered a URL, like "|anchor". This way UIWebView will think it's a real link and attempt the request when clicked. This solution obviously will only work if you are handling all the clicks yourself and hence can interpret the changed anchors. I can understand the reason for the behavior of UIWebView, but it is rather frustrating. The delegate really needs messages to match the onclick, etc. handlers.

GrafikRobot