Attempting to utilize a native function in Swift from wkwebview has been tough. Here's the progress I've made so far:
Swift Part
override func viewDidLoad() {
super.viewDidLoad()
// Additional setup after loading the view.
contentController.add(self, name: "backHomePage")
config.userContentController = contentController
self.webView = WKWebView(frame: self.containerView.bounds, configuration: config)
webView.navigationDelegate = self
self.containerView.addSubview(self.webView)
if let url = URL(string: assessmentLink) {
webView.load(URLRequest(url: url))
}
}
And
extension WebFormVC: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("javascript sending \(message.name), body: \(message.body)")
}
}
Javascript
function backHomePage(message) {
window.webkit.messageHandlers.backHomePage.postMessage(message);
}
The message parameter can be any string, such as "success"
Currently not receiving a call back in the userContentController didReceive
method
UPDATE
Tried sending data as key-value pairs, e.g.,
window.webkit.messageHandlers.backHomePage.postMessage({"message" :"Javascript to swift!"});
, but still no success.