加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:49:39 所属栏目:百科 来源:网络整理
导读:我需要在Swift代码中包含JavaScript代码才能调用signalR聊天,这可能吗?如果没有,我可以转换它吗? sendmessage是一个按钮. $(function () { // Declare a proxy to reference the hub. var chat = $.connection.chatHub; // Create a function that the hub
我需要在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代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读