Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebViewDelegate: webViewDidFinishLoad not called during in-page navigation

I have an app where users can navigate a pile of locally stored HTML files. I have a UIWebView, configured up correctly with a UIWebViewDelegate. Usually, when the user follows a link, shouldStartLoadWithRequest is called, followed by webViewDidFinishLoad a bit later.

But, if the link is pointing to an anchor on the same page as the one which is currently displayed, only shouldStartLoadWithRequest is called. webViewDidFinishLoad does not fire.

In a sense, I see that this might be expected behaviour, because in-page navigation should not require a page reload. However, I really need a place to hook into the call stack after in-page navigation is complete. The optimal solution would let me know when any sort of navigation has ended, both from another page, in-page and forward/backward actions.

My best hackaround so far has been to call performSelector: withObject: afterDelay: at the end of my shouldStartLoadWithRequest method, but I'm not happy with this.

Does anyone know how I can solve this correctly? Any insight appreciated!

like image 246
thomax Avatar asked Mar 27 '13 08:03

thomax


Video Answer


2 Answers

You can try to use NSURLConnectionDataDelegate, it allows you to handle incoming data. Maybe you can determine if the page is loaded manually by adding a sign to your html files.

NSURLConnectionDataDelegate Reference

Edit: gist.github.com/buranmert/7304047 I wrote a piece of code and it worked, that may not be the way you wanted it to work but maybe it helps. Every time user clicks a URL with anchor, it creates another connection and as connection finishes loading web view loads the data, that marks the point where web view finished loading the page. As you use only local html files, I do not think creating connections will create problems

like image 105
Mert Buran Avatar answered Oct 11 '22 01:10

Mert Buran


What you are describing is intended behavior. Just as AJAX or resource requests are never passed to the delegate, only root page changes will ever hit webViewDidFinishLoad:. But I have good news, and it doesn't involve saving a bunch of money on car insurance.

Loads performed within an iFrame DO trigger the full delegate methods and this gives you a solution. You can use this mechanism to post a notification to the native code, just as is often done for console.log() as described in this post. Alternatively, Native Bridge would work well to call into your Objective C code from JavaScript.

like image 36
Holly Avatar answered Oct 11 '22 01:10

Holly