如何检测 UIWebView 中的重定向
我在我的应用程序中使用 UIWebView 作为嵌入式浏览器。我遇到的问题是跟踪应在 URL 栏中显示的 URL。
当进行 Google 搜索时,结果页面通常会生成如下链接:
当用户单击此链接时,UIWebView 首先报告此链接,然后报告 shouldStartLoadWithRequest:navigationType:
中的重定向链接。
我如何判断这是重定向,而不是为页面上的图像或其他元素加载某些补充站点?按照目前的情况,我的网址栏在上面的示例中显示来自 Google 的长链接,而不是更新为维基百科 URL。
I am using a UIWebView as an embedded browser within my app. The problem I have is tracking the URL that should be displayed in the URL bar.
When doing a Google search, the results page often generates links like this:
When the user clicks this link, the UIWebView first reports this link and then the redirected link in shouldStartLoadWithRequest:navigationType:
.
How can I tell this is a redirect as opposed to some supplementary site being loaded for images or other elements on the page? As it stands my URL bar is displaying the long link from Google in the above example rather than updating to the Wikipedia URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您真正能做的最好的事情就是观察
webView:shouldStartLoadWithRequest:navigationType:
委托方法。我假设重定向属于UIWebViewNavigationTypeOther
。The best you can really do is observe the
webView:shouldStartLoadWithRequest:navigationType:
delegate method. I assume that redirects fall underUIWebViewNavigationTypeOther
.现在有点晚了,但我会使用 webViewDidFinishLoad 和 webViewDidStartLoad 委托方法来检测重定向,如下所示:
A little late now but I would use the webViewDidFinishLoad and the webViewDidStartLoad delegate methods to detect redirects as follows:
我正在解决同样的问题。最初,我遵循此处的建议,并使用 maindocumenturl 手动将 url 输入到历史列表中以进行后退/前进导航。但是,无论您是否从 didstartload 或 didfinishload 获取 url,这都没有给出非常准确的 url。如果你想感受我的痛苦,尝试通过谷歌搜索导航,你会看到我在说什么,maindocumenturl 在那种环境下完全没用。相比之下,与 webviewdidfinishload 一起使用的绝对 url 属性效果更好,并且实际上允许用户创建可用的历史记录。这是我的两分钱。
I am working through this same issue. Originally I followed the advice here and used maindocumenturl for manually inputting urls into a history list for back/forward navigation. However, this didn't give very accurate urls, whether or not you got the url from didstartload or didfinishload. If you want to feel my pain, try navigate through a google search and you will see what I am talking about, maindocumenturl is completely useless in that environment. By contrast, the absolute url property used with webviewdidfinishload works much better, and actually lets the user create a usable history. That's my two cents.