Question

I want to do some extra work after the webpage is loaded, so I add some code in webViewDidFinished, but it seems in this situation it's not working.

Situation:I visit a web page with UIWebview then click a link to another, after that, I run

[webview goback];

It seems the page was loaded from cache, only

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

is called,

- (void)webViewDidFinishLoad:(UIWebView *)webView;

is not called.

Was it helpful?

Solution 2

However quirky, I suspect that this is correct behaviour. From the Apple doc:

webViewDidFinishLoad:

Sent after a web view finishes loading a frame.

My bet is that the UIWebView caches a certain number of pages, and the goBack and goForward methods do not guarantee that locations stored in the back-forward list are reloaded. And thus does not fire webViewDidFinishLoad.

But even though this is might be correct behaviour, I would certainly agree that this is bad API design. There should definitely be a finishLoad-ish method to hook into for back/forward/in-page navigation.

OTHER TIPS

True. There's still a way to get informed about a change to the history stack:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(webViewHistoryDidChange:)
                                             name:@"WebHistoryItemChangedNotification"
                                           object:nil];

Since UIWebView is not KVO compliant on canGoBack this seems to be the only way to enable/disable a potential back button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top