2012-03-14 72 views
0

取决于网址,有时多个URL可以被加载,例如,假设你的URL http://www.zagat.com/加载到一个UIWebView然后shouldStartLoadWithRequest:将网址被调用如下无数次:如何检测何时UIWebView真的完成加载时涉及重定向?

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 to shouldShartLoadWithRequest)

由于加载单个url可能会导致多个url被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被传递tomeStLoadWithRequest作为document.location.href更改的结果,但它不会作为最终的url传递。

因此,怎样才能知道页面何时完全加载完成,并且不会再有更多的URL被处理?

回答

0

我只有一个建议 - 使用一些连接类来获取数据并将其显示在Web视图中。好处是您可以完全控制连接,例如您可以跟踪响应代码3xx并知道发生了重定向。不幸的是,切断了一些文档(如MS Office文件)的简单支持,并且您可能无法完成许多额外的工作。作为捷径,您可以尝试使用已重定向支持声明的GDataHTTPFetcher

基本上,据我所知,HTTP重定向的响应的状态代码200意味着你在重定向路线的末尾,但没有其他方式我知道获得响应状态码。

+0

感谢您的回复 - 您是什么意思,切断了一些文件的简单支持? – Gruntcakes 2012-03-14 22:26:13

+0

假设你想在UIWebView上显示一个MS Word文档,只需要两种方法来完成它:afaik:直接从Web上加载它[webview loadRequest:它工作得很好,但如果我有文件的NSData内容我有将文件保存在沙盒中,并使用loadRequest和本地url请求,因为loadData:从我的经验来看,它不是很好(它给了我一个空视图,指定了正确的mime类型,而loadRequest即使是docx文件也会显示为iOS 3.1)。 – 2012-03-14 22:41:41

相关问题