5

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.

jeswang
  • 1,017
  • 14
  • 26
  • have you added UIWebViewDelegate in .h file and binded your web view with File's owner? – Shah Paneri Mar 23 '13 at 07:08
  • on, @ShahPaneri must I? the function runs except this situation. – jeswang Mar 23 '13 at 07:09
  • @ShahPaneri added it. It didn't work. – jeswang Mar 23 '13 at 07:12
  • `- (void) webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"finish loding.."); }` Please log something in this method.So u can be more specific about this method is called or not. :) – Shah Paneri Mar 23 '13 at 07:13
  • @ShahPaneri Actually I did do some log and also I set a breakpoint there to check it... – jeswang Mar 23 '13 at 07:14
  • i have put one back button and on the click of that i have called `[webview goback];` and in my app i have used -`(void)webViewDidStartLoad:(UIWebView *)webView{ NSLog(@"load start...");}` and `- (void) webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"finish loding..");}`.My both methods are working while clicking on back.!! – Shah Paneri Mar 23 '13 at 07:22
  • @ShahPaneri try http://detail.tmall.com/item.htm?spm=a230r.1.10.1.kEUNrf&id=19955188752&ad_id=&am_id=&cm_id=&pm_id= to another link and then back. – jeswang Mar 23 '13 at 07:27

2 Answers2

5

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.

stigi
  • 6,661
  • 3
  • 40
  • 50
  • Xcode errors out stating it cannot find WebHistoryItemChangedNotification. I did add import WebKit to my uiwebView controller . Any thoughts? – shrutim Jun 29 '15 at 18:13
  • 1
    @shrutim: This is a hack which listens to a private notification. Therefore you need to subscribe to the `@"WebHistoryItemChangedNotification"` as a string, not as a symbol. – stigi Jul 07 '15 at 23:54
  • erm doesn't seem to be firing for me. Does anyone know if this is for UIWebView only or whether it works for WKWebKit also (which I'm using). – wuf810 Aug 04 '16 at 16:03
1

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.

thomax
  • 9,213
  • 3
  • 49
  • 68