JS在Swift中调用函数

时间:2023-01-17 00:00:15

I know how to send message from JS to native IOS swift app:

我知道如何从JS发送消息到本机IOS swift应用程序:

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:

说我想在JS中使用类似的东西:

webkit.messageHandlers.someFunctionName()

The reason i am asking is that In Android thats how it works

我问的原因是在Android中它是如何工作的

2 个解决方案

#1


3  

WKWebView supports only the raw message passing interface. You have to wrap it for complex interactions between JS and native.

WKWebView仅支持原始消息传递接口。你必须将它包装在JS和native之间的复杂交互中。

I created a project named XWebView which offers language binding styled API based on the raw message passing of WKWebView. It's written in Swift.

我创建了一个名为XWebView的项目,该项目基于WKWebView的原始消息传递提供语言绑定样式API。它是用Swift编写的。

For example, you can write a plugin in Swift:

例如,您可以在Swift中编写插件:

class Plugin : NSObject {
    func someFunctionName() {
        doSomeThing()
    }
}

Expose an instance of the plugin to JavaScript before loading HTML:

在加载HTML之前将插件的实例暴露给JavaScript:

let webView = WKWebView(frame: frame, configuration: WKWebViewConfiguration())
webView.loadPlugin(Plugin(), namespace: "foo")

Call the function from JavaScript:

从JavaScript调用该函数:

window.foo.someFunctionName()

It's really easy and straightforward. For more details, please check the project's page.

这真的很容易和直截了当。有关详细信息,请查看项目页面。

#2


0  

You could try to use the url parameters, like you would connect to PHP using swift

您可以尝试使用url参数,就像使用swift连接到PHP一样

Here is an example of how to get the url parameters in javascript: Get url parameter jquery Or How to Get Query String Values In js

以下是如何在javascript中获取url参数的示例:获取url参数jquery或如何在js中获取查询字符串值

Then when you run the javascript you can check, in the check what the url parameters are and then run a function based on that

然后当你运行javascript时,你可以检查一下url参数是什么,然后根据它运行一个函数

like this the url is example.com we use the parameter command, to say what function to run

像这样url是example.com我们使用参数命令来说明要运行的函数

example.com?command=foo

then we should have someting like this in the javascript

那么我们应该在javascript中有这样的东西

if(command='foo'){
   //run function
   runFoo();
}

Then you could also use the url parameters to set parameters into the function

然后您还可以使用url参数将参数设置到函数中

If you choose PHP

如果你选择PHP

if you choose to use php, this is the same thing you can do with $_GET['command'];

如果你选择使用php,这与你可以用$ _GET ['command']做同样的事情;

#1


3  

WKWebView supports only the raw message passing interface. You have to wrap it for complex interactions between JS and native.

WKWebView仅支持原始消息传递接口。你必须将它包装在JS和native之间的复杂交互中。

I created a project named XWebView which offers language binding styled API based on the raw message passing of WKWebView. It's written in Swift.

我创建了一个名为XWebView的项目,该项目基于WKWebView的原始消息传递提供语言绑定样式API。它是用Swift编写的。

For example, you can write a plugin in Swift:

例如,您可以在Swift中编写插件:

class Plugin : NSObject {
    func someFunctionName() {
        doSomeThing()
    }
}

Expose an instance of the plugin to JavaScript before loading HTML:

在加载HTML之前将插件的实例暴露给JavaScript:

let webView = WKWebView(frame: frame, configuration: WKWebViewConfiguration())
webView.loadPlugin(Plugin(), namespace: "foo")

Call the function from JavaScript:

从JavaScript调用该函数:

window.foo.someFunctionName()

It's really easy and straightforward. For more details, please check the project's page.

这真的很容易和直截了当。有关详细信息,请查看项目页面。

#2


0  

You could try to use the url parameters, like you would connect to PHP using swift

您可以尝试使用url参数,就像使用swift连接到PHP一样

Here is an example of how to get the url parameters in javascript: Get url parameter jquery Or How to Get Query String Values In js

以下是如何在javascript中获取url参数的示例:获取url参数jquery或如何在js中获取查询字符串值

Then when you run the javascript you can check, in the check what the url parameters are and then run a function based on that

然后当你运行javascript时,你可以检查一下url参数是什么,然后根据它运行一个函数

like this the url is example.com we use the parameter command, to say what function to run

像这样url是example.com我们使用参数命令来说明要运行的函数

example.com?command=foo

then we should have someting like this in the javascript

那么我们应该在javascript中有这样的东西

if(command='foo'){
   //run function
   runFoo();
}

Then you could also use the url parameters to set parameters into the function

然后您还可以使用url参数将参数设置到函数中

If you choose PHP

如果你选择PHP

if you choose to use php, this is the same thing you can do with $_GET['command'];

如果你选择使用php,这与你可以用$ _GET ['command']做同样的事情;