Scrungepipes
Scrungepipes

Reputation: 37580

How to detect when a UIWebView really has finished loading when redirection is involved?

Depending upon the url, sometimes multiple urls can be loaded, for example suppose you load the url http://www.zagat.com/ into UIWebView then shouldStartLoadWithRequest: will get called numerous times with the urls as follows:

http://www.zagat.com/ 
http://pixel.fetchback.com/serve/fb/pdj?cat=&name=landing&sid=3997 
https://m.google.com/app/plus/poswidget/?hl=en-US&jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1#url=http%3A%2F%2Fwww.zagat.com%2F&size=small&count=true&id=I1_1331701330274&parent=http%3A%2F%2Fwww.zagat.com&rpctoken=375018132&_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart 

http://pixel.fetchback.com/serve/fb/blank 
https://www.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1#parent=https%3A%2F%2Fm.google.com&rpctoken=809526044 
https://m.google.com/app/plus/poswidget/?hl=en-US&jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1&jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1#bub=1&url=http://www.zagat.com/&id=I2_1331701333045&parent=http%3A%2F%2Fwww.zagat.com&rpctoken=257947967&_methods=_onopen%2C_ready%2C_onclose%2C_close%2C_open%2C_resizeMe%2C_renderstart

(obtained by adding a NSLog to shouldShartLoadWithRequest)

As loading a single url can lead to multiple urls being loaded by UIWebView how is it possible to know when the page you originally loaded actually has fully finished loaded?

I tried injecting the following javascript into the page:

var script = document.createElement('script');  
script.type = 'text/javascript';  
script.text = window.onload = function DOMHasLoaded() {
document.location.href = "mydomain://anything.stuff";
};

But when executed the order of urls passed to shouldStartLoadWithRequest is:

http://www.zagat.com/ 
http://pixel.fetchback.com/serve/fb/pdj?cat=&name=landing&sid=3997 
http://pixel.fetchback.com/serve/fb/blank 
https://m.google.com/app/plus/poswidget/?hl=en-US&jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1#url=http%3A%2F%2Fwww.zagat.com%2F&size=small&count=true&id=I1_1331701452309&parent=http%3A%2F%2Fwww.zagat.com&rpctoken=248478838&_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart 
https://www.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1#parent=https%3A%2F%2Fm.google.com&rpctoken=230813838 
mydomain://anything.stuff 
https://m.google.com/app/plus/poswidget/?hl=en-US&jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1&jsh=m%3B%2F_%2Fapps-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3Dt_fRXLZrGUk.en_US.%2Fsv%3D1%2Fam%3D!wJ3zzZyr7MyIPjLesA%2Fd%3D1#bub=1&url=http://www.zagat.com/&id=I2_1331701453238&parent=http%3A%2F%2Fwww

i.e. mydomain://anything.stuff gets passed to shouldStartLoadWithRequest as a result of the document.location.href change but it doesn't get passed as the final url.

So therefore how is it possible to know when the page has finally finished loading in full and there will be no more urls processed?

Upvotes: 0

Views: 1027

Answers (1)

A-Live
A-Live

Reputation: 8944

Only one suggestion i have - to use some connectivity class to get the data and display it at your web view. The benefit is that you are in full control of the connection, for example you can track the response codes 3xx and be aware that redirection happened. Unfortunately that cuts off the easy support of some documents (like MS Office files), and there's much extra work you probably don't work to do. As a shortcut, you could try to use a ready GDataHTTPFetcher which has redirects support declared.

Basically as i understand HTTP redirection the status code 200 of the response means you are in the end of the redirection route, but there's no other way i know to get the response status code.

Upvotes: 0

Related Questions