Jaka Jančar
Jaka Jančar

Reputation: 11606

How to detect page change in UIWebView and do something before it starts loading?

I need to do something on each page change, before that page starts loading/executing.

First, I tried using -webView:shouldStartLoadWithRequest: but that won't work, because it's also called for XHR requests and iframe requests.

Second, I tried using -webViewDidStartLoad: and comparing webView.request to it's previous value, to see if it changed. This doesn't work, because webView.request is updated only some time after the call.

Any more ideas?

Upvotes: 8

Views: 3972

Answers (3)

Lee Higgins
Lee Higgins

Reputation: 215

The request object in webView:shouldStartLoadWithRequest: has two properties,

BOOL iframe = ![request.URL isEqual:request.mainDocumentURL];

If the URL to load is not equal to the mainDocumentURL you are loading something other than main doc (probably an iFrame).

Upvotes: 10

hatfinch
hatfinch

Reputation: 3095

Have you tried using KVO to observe UIWebView.request?

Upvotes: 1

David Sowsy
David Sowsy

Reputation: 1680

Make a Category or extend UIWebview that overrides loadRequest, doing your operation first and then doing the standard loadRequest.

Upvotes: 0

Related Questions