I know how to send message from JS to native IOS swift app:
func userContentController(userContentController: WKUserContentController!, didReceiveScriptMessage message: WKScriptMessage!) {
println("JavaScript is sending a message \(message.body)")
}
and JS:
webkit.messageHandlers.callbackHandler.postMessage("Hello from JavaScript");
My question: is it possible to directly call a function in the native app rather then receiving the message in my example code.
say i want in the JS to use something like:
webkit.messageHandlers.someFunctionName()
The reason i am asking is that In Android thats how it works
You could try to use URL query variables, like you would connect to PHP using Swift.
Here is an example of how to get query variables in Javascript: Get url parameter jquery Or How to Get Query String Values In js
Then when you run the Javascript you can check what the URL query variables are and then run a function based on that.
For example, we use the variable
command
below, to say what function to run:then do something like this in Javascript:
Then you could also use URL query variables to specify arguments for the function.
If you choose PHP
If you choose to use PHP, you can do the same thing with
$_GET['command'];
.