我可以在 Swift 代码中运行 JavaScript 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37434560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I run JavaScript inside Swift code?
提问by user1019042
I need to include JavaScript code in Swift code to be able to call a signalR chat, is that possible? If not, can I convert it?
我需要在 Swift 代码中包含 JavaScript 代码才能调用 signalR 聊天,这可能吗?如果没有,我可以转换它吗?
sendmessage
is a button.
sendmessage
是一个按钮。
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// some code
};
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send('name', 'message');
});
});
});
and the signalr
code is:
和signalr
代码是:
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
Update #1:
更新 #1:
changed question a little bit so it is not confusing per @MartinR
稍微改变了问题,所以@MartinR 不会让人感到困惑
回答by Daniel
Last tested with Swift 5.1
最后用 Swift 5.1 测试
Here is an example you can run in Playground to get you started:
以下是您可以在 Playground 中运行以帮助您入门的示例:
import JavaScriptCore
let jsSource = "var testFunct = function(message) { return \"Test Message: \" + message;}"
var context = JSContext()
context?.evaluateScript(jsSource)
let testFunction = context?.objectForKeyedSubscript("testFunct")
let result = testFunction?.call(withArguments: ["the message"])
result
would be Test Message: the message
.
result
会Test Message: the message
。
You also can run JavaScript code within a WKWebViewcalling evaluate?Java?Script(_:?completion?Handler:?).
您还可以在WKWebView 中运行 JavaScript 代码调用evaluate?Java?Script(_:?completion?Handler:?)。
You can also run JavaScript within a UIWebViewby calling string?By?Evaluating?Java?Script(from:?), but note that that method has been deprecatedand is marked as iOS 2.0–12.0.
您还可以通过调用string?By?Evaluating?Java?Script(from:?)在UIWebView 中运行 JavaScript ,但请注意,该方法已被弃用并标记为 iOS 2.0–12.0。
回答by Ashish
Using?JavaScriptCore frameworkinclude JavaScript code in Swift code.
使用?JavaScriptCore 框架在 Swift 代码中包含 JavaScript 代码。
The class that you'll be dealing the most with, is?JSContext. This class is the actual environment (context) that executes your JavaScript code.
你最常接触的课程是?JSContext。此类是执行 JavaScript 代码的实际环境(上下文)。
All values in?JSContext, are JSValue objects, as the?JSValue?class represents the datatype of any JavaScript value. That means that if you access a JavaScript variable and a JavaScript function from Swift, both are considered to be?JSValue?objects.
所有值都在?JSContext是JSValue 对象,因为?JSValue?class 代表任何 JavaScript 值的数据类型。这意味着如果你从 Swift 访问一个 JavaScript 变量和一个 JavaScript 函数,两者都被认为是 JSValue 对象。
I strongly advise you to read the official documentation regarding the?JavaScriptCore framework.?
我强烈建议您阅读有关的官方文档?JavaScriptCore 框架.?
import JavaScriptCore
var jsContext = JSContext()
// Specify the path to the jssource.js file.
if let jsSourcePath = Bundle.main.path(forResource: "jssource", ofType: "js") {
do {
// Load its contents to a String variable.
let jsSourceContents = try String(contentsOfFile: jsSourcePath)
// Add the Javascript code that currently exists in the jsSourceContents to the Javascript Runtime through the jsContext object.
self.jsContext.evaluateScript(jsSourceContents)
}
catch {
print(error.localizedDescription)
}
}
more details refer this tutorial
更多细节参考本教程