目标是在包含在WKWebView
.
evaluateJavaScript
当其参数包含换行符时中断,这意味着永远不会调用回调函数。
为什么是这样?
userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
当用户按下网页上的按钮时调用。
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
let dict = message.body as! [String:AnyObject]
let callback = dict["callback"] as! String
// Fails
let serializedClipboard = "hello\n"
// Works
// let serializedClipboard = "hello"
webView!.evaluateJavaScript("\(callback)('\(serializedClipboard)')") { (object: Any?, error: Error?) -> Void in
print("Done invoking \(callback)")
}
}