The goal is to invoke a callback function inside a web page contained inside a WKWebView
.
evaluateJavaScript
breaks when its parameter contains the newline character, meaning the callback function never gets called.
Why is this?
userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
is invoked when the user presses a button on the web page.
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)")
}
}