如何在涉及重定向时检测UIWebView何时真正完成加载?

时间:2012-03-14 21:08:47

标签: ios uiwebview

根据网址的不同,有时可以加载多个网址,例如,假设您将网址http://www.zagat.com/加载到UIWebView中,那么应该使用网址多次调用theStartLoadWithRequest:如下所示:

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

(通过将NSLog添加到shouldShartLoadWithRequest获得)

由于加载单个网址可能会导致UIWebView加载多个网址,如何才能知道您最初加载的网页何时实际已完全加载?

我尝试将以下javascript注入页面:

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

但是当执行时,传递给shouldStartLoadWithRequest的url顺序为:

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

即。 mydomain://anything.stuff由于document.location.href更改而传递给shouldStartLoadWithRequest,但它不会作为最终URL传递。

因此,如何才能知道页面何时最终完成加载并且不再处理网址?

1 个答案:

答案 0 :(得分:0)

我只有一个建议 - 使用一些连接类来获取数据并在Web视图中显示它。好处是您可以完全控制连接,例如,您可以跟踪响应代码3xx并注意重定向发生。不幸的是,这会削弱对某些文档(如MS Office文件)的轻松支持,并且还有许多额外的工作可能无法完成。作为一种快捷方式,您可以尝试使用已重定向支持声明的就绪GDataHTTPFetcher

基本上,当我理解HTTP重定向时,响应的状态代码200意味着您处于重定向路由的末尾,但是我知道没有其他方式来获取响应状态代码。