zoukankan      html  css  js  c++  java
  • WKWebView不显示提示框(Swift)

    使用WKWebView的时候会出现明明自己做的一些页面有提示框, 为什么使用别人的页面提示框总是不显示, 其实很大部分原因是因为该提示框是通过JS调用的, 需要实现WKUIDelegate来进行监听

    // MARK: - WKUIDelegate
        
        // 监听通过JS调用警告框
        func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
            let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                completionHandler()
            }))
            self.present(alert, animated: true, completion: nil)
        }
        
        // 监听通过JS调用提示框
        func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
            let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                completionHandler(true)
            }))
            alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
                completionHandler(false)
            }))
            self.present(alert, animated: true, completion: nil)
        }
        
        // 监听JS调用输入框
        func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
            // 类似上面两个方法
        }
    

      这里需要注意, completionHandler一定要调用, 否则会出错!

  • 相关阅读:
    AutoMapper 使用实践
    项目重构之路
    Xamarin对Visual Studio用户免费 Xamarin SDK将开源
    php的文件上传及下载,附带显示文件及目录
    SMARTY静态缓存
    SMARTY的简单实例写法
    SMARTY的知识
    wampserver的使用配置
    php权限管理
    phpcms企业站的一些知识
  • 原文地址:https://www.cnblogs.com/Rinpe/p/5894141.html
Copyright © 2011-2022 走看看