美文网首页iOS踩过的坑系列
iOS踩过的坑之WKWebView

iOS踩过的坑之WKWebView

作者: Shaw1211 | 来源:发表于2019-04-24 14:33 被阅读0次

问题1

h5调用alert()方法,客户端不弹出alert

解决方法:

lazy var webView: XTWebView = {
        let temp = XTWebView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 64), configuration: self.config)
        temp.navigationDelegate = self
        temp.allowsBackForwardNavigationGestures = true
        temp.uiDelegate = self
        return temp
    }()
extension NNWebViewController: WKUIDelegate {
    //Alert弹框
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        let alert = UIAlertController(title: message, message: nil, preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "好的", style: UIAlertActionStyle.cancel) { (_) in
            completionHandler()
        }
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
    }
    //confirm弹框
    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
        let alert = UIAlertController(title: message, message: nil, preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "确定", style: UIAlertActionStyle.default) { (_) in
            completionHandler(true)
        }
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) { (_) in
            completionHandler(false)
        }
        alert.addAction(action)
        alert.addAction(cancelAction)
        present(alert, animated: true, completion: nil)
    }
    //TextInput弹框
    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
        let alert = UIAlertController(title: "", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        alert.addTextField { (_) in}
        let action = UIAlertAction(title: "确定", style: UIAlertActionStyle.default) { (_) in
            completionHandler(alert.textFields?.last?.text)
        }
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
    }
}

问题2

h5页面中不能弹出menuItem,即不能进行文字的复制搜索等功能。
此问题出现在6s及以上机型中,原因为与3DTouch功能冲突

解决方法:

在控制器中添加以下代码:

    override var traitCollection: UITraitCollection {
        if #available(iOS 9.0, *) {
            return UITraitCollection.init(forceTouchCapability: .unavailable)
        } else {
            return UITraitCollection.init()
        }
    }

相关文章

网友评论

    本文标题:iOS踩过的坑之WKWebView

    本文链接:https://www.haomeiwen.com/subject/skrggqtx.html