objective-c 在 UIWebView 中打开弹出链接,可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1245224/
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
Opening popup links in UIWebView, possible?
提问by Jasarien
I have a UIWebView which I'm using as an embedded browser within my app.
我有一个 UIWebView,我在我的应用程序中将其用作嵌入式浏览器。
I've noticed that links in webpages that open new windows are ignored without any call into my code.
我注意到打开新窗口的网页中的链接在没有调用我的代码的情况下被忽略。
I've tried breakpointing on
我试过断点
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
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 并正常加载它?
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.
我对在应用程序本身中显示弹出窗口不感兴趣,我只希望将要在弹出窗口中加载的任何内容的 URL 加载到主 web 视图本身中。
Is this possible?
这可能吗?
Thanks!
谢谢!
采纳答案by dmercredi
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.
我也遇到了这个问题,HTML 重写是我能想到的最好的解决方案。我用这种方法遇到的最大问题是,Web 浏览器在调用 webViewDidFinishLoad: 方法之前最多可交互几秒钟,因此链接似乎中断了几秒钟,直到它们被重写。
There's three areas that I rewrote: links, form posts, and calls to window.open().
我重写了三个区域:链接、表单帖子和对 window.open() 的调用。
I used a similar approach to the first code snipped in Jasarian'sanswer 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:
我使用了与Jasarian 的答案中截断的第一个代码类似的方法,通过迭代标签和表单来覆盖链接和表单的目标。为了覆盖 window.open,我使用了类似于以下的代码:
var oldWindowOpen = window.open;
window.open = function(url, sName, sFeatures, bReplace) {
oldWindowOpen(url, '_self');
};
回答by Jasarien
So after a small amount of research, it's clear that the UIWebView class purposefully ignores links that will open in a new window (either by using the 'target' element on the a tag or using javascript in the onClick event).
因此,经过少量研究后,很明显 UIWebView 类故意忽略将在新窗口中打开的链接(通过使用 a 标记上的“目标”元素或在 onClick 事件中使用 javascript)。
The only solutions I have found are to manipulate the html of a page using javascript. While this works for some cases, it's not bulletproof. Here are some examples:
我发现的唯一解决方案是使用 javascript 操作页面的 html。虽然这适用于某些情况,但它不是防弹的。这里有些例子:
links = document.getElementsByTagName('a');
for (i=0; i<links.length; i++)
{
links[i].target='_self';
}
This will change all links that use the 'target' element to point at _self - instead of _blank or _new. This will probably work across the board and not present any problems.
这将更改所有使用 'target' 元素的链接指向 _self - 而不是 _blank 或 _new。这可能会全面工作并且不会出现任何问题。
The other snippet I found followed the same idea, but with the onClick event:
我发现的另一个片段遵循相同的想法,但使用 onClick 事件:
links = document.getElementsByTagName('a');
for (i=0; i<links.length; i++)
{
links[i].onclick='';
}
This one is just plain nasty. It'll only work if the link tag has it's href element correctly set, and only if the onclick event is used to open the new window (using window.open() or something similar). The reasons why it is nasty shouldn't need explaining, but one example would be if the onClick is used for anything other than opening a window - which is a very common case.
这个简直太恶心了。只有当链接标签正确设置了它的 href 元素,并且只有当 onclick 事件用于打开新窗口(使用 window.open() 或类似的东西)时,它才会起作用。它令人讨厌的原因不需要解释,但一个例子是如果 onClick 用于打开窗口以外的任何其他事情 - 这是一种非常常见的情况。
I guess one could go further with this and start doing some string matching with the onClick method, and check for window.open(), but again, this is really far from ideal.
我想可以更进一步,开始用 onClick 方法做一些字符串匹配,并检查 window.open(),但同样,这真的很不理想。
回答by Ken
Here's how I get twitter links to work (i.e. link to pages that try to open with new windows):
以下是我如何使 twitter 链接工作(即链接到尝试使用新窗口打开的页面):
-(BOOL)webView:(UIWebView *)mainWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
//Allows for twitter links
[self.mainWebView loadRequest:request];
return NO;
}
return YES;
}
回答by Hemanshu Liya
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
theConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
webView1 = [[WKWebView alloc] initWithFrame:self.webView.frame configuration:theConfiguration];
webView1.navigationDelegate = self;
webView1.UIDelegate = self;
[self.view addSubview:webView1];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSString *htmlString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"htmlString: %@", htmlString);
[webView1 loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"your url"];
}];
[dataTask resume];

