-
Notifications
You must be signed in to change notification settings - Fork 13
Description
Hello, is it possible to return String value from Swift to JS?
For example, I would like to do something like this:
@objc protocol JavaScriptInterface {
func someFunc() -> String
}
extension ViewController: JavaScriptInterface {
func someFunc() -> String {
return "alert('close.')" //so alert would be presented in webView when this method is called.
}
}
or
extension ViewController: JavaScriptInterface {
func someFunc() -> String {
return "Something" //return some string to JS and let it handle it.
}
}
In JS, I am calling it like this, for example when button is tapped:
return iosListener.someFunc();
//iosListener is a name of userContentController
This is how I implement webView:
webView = WKWebView(frame: view.bounds, configuration: setupWebViewConfiguration())
webView.navigationDelegate = self
webView.uiDelegate = self
let javaScriptController = WKJavaScriptController(name: "iosListener", target: self, bridgeProtocol: JavaScriptInterface.self)
webView.javaScriptController = javaScriptController
webView.prepareForJavaScriptController()
view.addSubview(webView)
//then load the url
I am getting this error: [WKJavaScriptController] An unimplemented method has been called. (selector: someFunc)
Also worth mentioning, my script where I call this method is being called by evaluateJavascript(), when url is loaded. I am not using WKUserScript. My JS is on the web, and I am using evaluateJavascript() to return Swift method.