简体   繁体   English

阻止UIWebView警报

[英]Blocking UIWebView alerts

I have an app that opens webpages from a variety of domains. 我有一个应用程序可以打开来自多个域的网页。 Some of those domains have embedded scripts that display alerts like You're the 1 millionth visitor to this website. You won >>nothing<<" 这些域中的某些域嵌入了显示警报的脚本,例如You're the 1 millionth visitor to this website. You won >>nothing<<" You're the 1 millionth visitor to this website. You won >>nothing<<" . You're the 1 millionth visitor to this website. You won >>nothing<<"

I've decided to hijack window.alert method calls to do nothing so that no malicious alerts can be displayed by said websites. 我决定劫持window.alert方法调用,使其无所事事,以使上述网站无法显示恶意警报。

I block the alerts in both - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType and - (void)webViewDidFinishLoad:(UIWebView *)webView to prevent websites displaying alerts before the page has finished loading. 我在- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType- (void)webViewDidFinishLoad:(UIWebView *)webView中均阻止警报,以防止网站在页面前显示警报已完成加载。

Some crash reports indicate that calling webView stringByEvaluatingJavaScriptFromString:... from shouldStartLoadWithRequest causes the app to crash. 一些崩溃报告表明,从shouldStartLoadWithRequest调用webView stringByEvaluatingJavaScriptFromString:...会导致应用崩溃。 Not always, just sometimes. 并非总是如此,只是有时。 Any idea why that might be? 知道为什么会这样吗?

Is there a way to prevent the alerts displaying in the first place without doing the manual hijacking? 有没有一种方法可以防止在没有进行手动劫持的情况下首先显示警报? I'd love to use SFSafariViewController but that's not an option for various constraints unfortunately. 我很想使用SFSafariViewController,但是不幸的是,这并不是各种约束的选择。

Why not use WKWebView? 为什么不使用WKWebView? WKWebView hooks JS's alert and do nothing by default. WKWebView挂钩JS的警报,默认情况下不执行任何操作。

FYI: 仅供参考:

If you want to show alert when JS's alert is called, implement WKUIDelegate's 如果要在调用JS的警报时显示警报,请实施WKUIDelegate的

webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) 

and show dialog by yourself. 并自己显示对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM