I want to use the Async functionality offered by WKWebView outside web view. The JS Context option does not provide the Async functionality.
In WKWebView, I write my logic as follows.
func swiftFunc1() {
webView.evaluateJavaScript("jsFunc1(), completionHandler: nil)
}
In javascript code I post a message to swift
function jsFunc1() {
window.webkit.messageHandlers.myMsg.postMessage("call swiftFunc2");
}
The swift code can then call appropriate JS callback as a part of handling message.
But this is dependent upon the webview being the foreground view. If I want to use the JS logic independent of the webview, JSContext is the option. I tried following
func swiftFunc1() {
myCtxt = JSContext()
exportedToJS = exportToJS() //confirms to JSExport and uses @objc
myCtxt.setObject(exportedToJS.self, forKeyedSubscript: "swiftIface")
myFunc = myCtxt.objectForKeyedSubscript("jsFunc1")
myFunc.callWithArguments(nil)
}
Now in javascript code I cannot post a message to swift. If I try to call a swift function as follows, code gets stuck forever.
function jsFunc1() {
swiftIface.swiftFunc2() // This creates a deadklock
}
How can I achieve either of the following without "returning" from the called Javascript function jsFunc1()?
Either post a message to swift so that it can take appropriate action
Or call a swift function so that the appropriate action is taken