2012-03-14 74 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

相關問題