Tuesday, February 28, 2017

How to correctly listen to navigation changes on UIWebView?

Leave a Comment

I noticed that the [UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:] is only called with traditional <a href="..."> links and not single page web app JavaScript navigations.

For example, if I go to m.facebook.com in a UIWebView and start tapping on the top nav (see screenshot below) it never triggers that delegate method.

The reason I want a method to hook into is because I want to add back and forward buttons above the UIWebView but I need to know when to enable/disable those buttons. Is there some other delegate method I should use to correctly listen to navigation changes?

Update: I should also note that mobile Safari correctly updates the back/forward button state on the Facebook mobile site, so I'd hope Apple exposes a way for developers to do this as well.

enter image description here

1 Answers

Answers 1

It seems like this is simply not supported by UIWebViewDelegate, nor WKNavigationDelegate (which I just tested with facebook). There are some options listed in this answer:

  1. javascript injection to capture the page changes (read up on history.pushState).
  2. KVO to watch for changes to the webview (like the scrollview resizing)
  3. watching for the title change, as above.
  4. checking for updates every time the user taps the view.

Update

I also tried this answer on facebook, which involves injecting javascript to catch XMLHTTPRequests during each new page load. It sort of does what you're asking for, but is a bit of a hack. Some of the facebook tabs don't work properly with a very simple implementation, but you may be able to work around the issues.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment