我可以在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代码

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...