将变量从Swift传递到javascript函数

编程入门 行业动态 更新时间:2024-10-10 21:25:44
本文介绍了将变量从Swift传递到javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用JSContext将变量传递给javascript函数时遇到麻烦.错误显示stringToSend未定义:

Having trouble using JSContext to pass a variable to a javascript function. The error says stringToSend is undefined:

func sendSomething(stringToSend : String) { appController?.evaluateInJavaScriptContext({ (context) -> Void in context.evaluateScript("myJSFunction(stringToSend)") }, completion: { (evaluated) -> Void in print("we have completed: \(evaluated)") }) }

推荐答案

这是我喜欢从Swift到Java进行通信的方式:

Here is how I like to communicate from Swift to Javascript:

func sendSomething(stringToSend : String) { appController?.evaluateInJavaScriptContext({ (context) -> Void in //Get a reference to the "myJSFunction" method that you've implemented in JavaScript let myJSFunction = evaluation.objectForKeyedSubscript("myJSFunction") //Call your JavaScript method with an array of arguments myJSFunction.callWithArguments([stringToSend]) }, completion: { (evaluated) -> Void in print("we have completed: \(evaluated)") }) }

在调用此方法时,请确保在您的JavaScript上下文中实现了 myJSFunction .

Make sure myJSFunction is implemented in your javascript context when you call this method.

使用 callWithArguments 时,您的 stringToSend 字符串将自动映射到javascript字符串.

Your stringToSend String will automatically be mapped to a javascript string when using callWithArguments.

更多推荐

将变量从Swift传递到javascript函数

本文发布于:2023-11-24 22:07:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   函数   Swift   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!