ios 如何检测 UIWebView 中的重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6144616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:06:29  来源:igfitidea点击:

How to detect redirect in a UIWebView

ioscocoa-touchurluiwebview

提问by zorro2b

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.

我在我的应用程序中使用 UIWebView 作为嵌入式浏览器。我遇到的问题是跟踪应显示在 URL 栏中的 URL。

When doing a Google search, the results page often generates links like this:

在进行 Google 搜索时,结果页面通常会生成如下链接:

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CC4QFjAA&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDog&rct=j&q=dogs&ei=27XeTc6RFIWEvAO858DHBQ&usg=AFQjCNGkEDcWzea4pSlurHhcuQfqFcp_pw

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CC4QFjAA&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDog&rct=j&q=dogs&ei=27XeTc6RFIWEvAO858DHBQ&usg=AFQlurcphphp

When the user clicks this link, the UIWebView first reports this link and then the redirected link in shouldStartLoadWithRequest:navigationType:.

当用户点击这个链接时,UIWebView首先报告这个链接,然后是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.

我怎么知道这是重定向,而不是为页面上的图像或其他元素加载一些补充站点?就目前而言,我的 URL 栏在上面的示例中显示来自 Google 的长链接,而不是更新到 Wikipedia URL。

回答by thelaws

The best you can really do is observe the webView:shouldStartLoadWithRequest:navigationType:delegate method. I assume that redirects fall under UIWebViewNavigationTypeOther.

您真正能做的最好的事情就是观察webView:shouldStartLoadWithRequest:navigationType:委托方法。我假设重定向属于UIWebViewNavigationTypeOther.

回答by PowerAktar

A little late now but I would use the webViewDidFinishLoadand the webViewDidStartLoaddelegate methods to detect redirects as follows:

现在有点晚了,但我会使用webViewDidFinishLoadwebViewDidStartLoad委托方法来检测重定向,如下所示:

- (void)webViewDidStartLoad:(UIWebView *)webView{

    myRequestedUrl= [webView.request mainDocumentURL];
    NSLog(@"Requested url: %@", myRequestedUrl);    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{  

    myLoadedUrl = [webView.request mainDocumentURL];
    NSLog(@"Loaded url: %@", myLoadedUrl);

   //psudocode
   if(myRequestedUrl is not the same as myLoadedUrl){
      doSomething
   }
}

回答by user2104778

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.

我正在解决同样的问题。最初我遵循了这里的建议,并使用 maindocumenturl 手动将 url 输入到历史列表中以进行后退/前进导航。但是,这并没有给出非常准确的网址,无论您是从 didstartload 还是 didfinishload 获得网址。如果您想感受我的痛苦,请尝试通过 google 搜索导航,您会看到我在说什么, maindocumenturl 在该环境中完全没有用。相比之下,与 webviewdidfinishload 一起使用的绝对 url 属性效果要好得多,实际上可以让用户创建可用的历史记录。那是我的两分钱。