我可以在Swift代码中运行JavaScript吗?

前端之家收集整理的这篇文章主要介绍了我可以在Swift代码中运行JavaScript吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在Swift代码中包含JavaScript代码才能调用signalR聊天,这可能吗?如果没有,我可以转换它吗?

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');
        });
    });
});

信号器代码是:

public void Send(string name,string message)
    {
        // Call the broadcastMessage method to update clients. 
        Clients.All.broadcastMessage(name,message);
    }

更新#1:

改变了一点问题,所以每个@MartinR都不会混淆

你可以在Swift里面运行JavaScript!

这是一个让你入门的例子:

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.callWithArguments(["the message"])

结果将是测试消息:消息.

您还可以通过调用string​By​Evaluating​Java​Script(from:​)或在调用evaluate​Java​Script(_:​completion​Handler:​)WKWebView内运行UIWebView中的JavaScript代码

原文链接:https://www.f2er.com/swift/320259.html

猜你在找的Swift相关文章