2016-05-25 11 views
13

SignalRのチャットを呼び出せるようにするには、SwiftコードにJavaScriptコードを含める必要がありますか?そうでない場合は、変換できますか?Swiftコード内でJavaScriptを実行できますか?

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

signalrコードは次のとおりです。

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

更新#1:

あなたが実行することができます@MartinR

答えて

27

あたり混乱しないように、少し疑問に変更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"]) 

resultTest Message: the messageだろう。

またstring​By​Evaluating​Java​Script(from:​)を呼び出したりWKWebView以内に私はそのコードを記述する必要がありevaluate​Java​Script(_:​completion​Handler:​)

+0

を呼び出すことによってUIWebView内のJavaScriptコードを実行することができますか?あなたはその完全な例として与えることができますか? –

+0

JSファイルをSwiftにどのようにインポートできるか知っていますか? – JmJ

+0

次のようなことができます: 'let jsSource = String.stringWithContentsOfFile(path +"/jsFile.js ")' – Daniel

関連する問題