简体   繁体   English

在 UIWebView 和 WkWebview 中禁用 javascript 警报?

[英]Disable javascript alerts in UIWebView & WkWebview?

As mentioned in this example for Android: Disable popups and alertboxes in android webview正如这个 Android 示例中提到的: 禁用 android webview 中的弹出窗口和警报框

WebChromeClient can be used to block javascript alerts. WebChromeClient 可用于阻止 javascript 警报。

Can someone provide a similar example for blocking window.alert|window.confirm|window.prompt javascript alerts in iOS WebViews & WkWebViews?有人可以提供一个类似的示例来阻止 iOS WebViews 和 WkWebViews 中的 window.alert|window.confirm|window.prompt javascript 警报吗?

in wkwebview this is implicit. 在wkwebview中,这是隐式的。 A js alert fires a delegate call only. js警报仅触发委托调用。 an alert would have to be triggered by the objc/swift code 警报将必须由objc / swift代码触发

@roshabh Yes,I just meet the same question, you must use UIDelegate methods of WKWebview to show the alert/conform/prompt effect. @roshabh 是的,我刚遇到同样的问题,您必须使用 WKWebview 的 UIDelegate 方法来显示警报/符合/提示效果。 code eg:代码例如:

webview?.uiDelegate = self

extension MessageNoticeViewController: WKUIDelegate {扩展 MessageNoticeViewController: WKUIDelegate {

func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
    let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)

    alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: { (action) in
        completionHandler()
    }))

    present(alertController, animated: true, completion: nil)
}

func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
    let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)

    alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: { (action) in
        completionHandler(true)
    }))

    alertController.addAction(UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .default, handler: { (action) in
        completionHandler(false)
    }))

    present(alertController, animated: true, completion: nil)
}

func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
    let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .alert)

    alertController.addTextField { (textField) in
        textField.text = defaultText
    }

    alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: { (action) in
        if let text = alertController.textFields?.first?.text {
            completionHandler(text)
        } else {
            completionHandler(defaultText)
        }
    }))

    alertController.addAction(UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .default, handler: { (action) in
        completionHandler(nil)
    }))

    present(alertController, animated: true, completion: nil)
}

} }

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

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