在 UIWebView 中打开弹出链接,可能吗?

Opening popup links in UIWebView, possible?(在 UIWebView 中打开弹出链接,可能吗?)

本文介绍了在 UIWebView 中打开弹出链接,可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIWebView,在我的应用中用作嵌入式浏览器.

I have a UIWebView which I'm using as an embedded browser within my app.

我注意到打开新窗口的网页中的链接会被忽略,而无需调用我的代码.

I've noticed that links in webpages that open new windows are ignored without any call into my code.

我试过断点

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

然后选择一个会打开一个弹出窗口的链接,并且断点永远不会被命中.我能做些什么来拦截弹出链接的选择并获取 URL 并正常加载它吗?

and then selecting a link that would open a popup window, and the breakpoint is never hit. Is there anything I can do to intercept that selection of the popup link and get the URL and just load it normally?

我对在应用程序本身中显示弹出窗口不感兴趣,我只想将要在弹出窗口中加载的任何内容的 URL 加载到主 web 视图本身中.

I'm not interested in displaying a popup window in the app itself, I just want the URL of whatever is going to be loaded in the popup window to load in the main webview itself.

这可能吗?

谢谢!

推荐答案

我也遇到了这个问题,HTML 重写是我能想到的最好的解决方案.我在使用这种方法时遇到的最大问题是,在调用 webViewDidFinishLoad: 方法之前,Web 浏览器最多可以交互几秒钟,因此链接似乎会中断几秒钟,直到它们被重写.

I ran into this as well, and HTML rewriting was the best solution I could come up with. The biggest issue that I ran into with that approach is that the web browser is interactive for up to a couple of seconds until the webViewDidFinishLoad: method is called, so the links seem to be broken for a few seconds until they're rewritten.

我重写了三个区域:链接、表单帖子和对 window.open() 的调用.

There's three areas that I rewrote: links, form posts, and calls to window.open().

我对 Jasarian's 答案中的第一段代码使用了类似的方法,通过迭代覆盖链接和表单的目标在标签和表格上.为了覆盖 window.open,我使用了类似于以下的代码:

I used a similar approach to the first code snipped in Jasarian's answer to overwrite the target for links and forms by iterating over tags and forms. To override window.open, I used code similar to the following:

var oldWindowOpen = window.open;
window.open = function(url, sName, sFeatures, bReplace) {
  oldWindowOpen(url, '_self');
};

这篇关于在 UIWebView 中打开弹出链接,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 UIWebView 中打开弹出链接,可能吗?

基础教程推荐