目标C将数据发送到phonegap并调用javascript函数 [英] Objective C send data to phonegap and call javascript function
问题描述
我正在通过使用phonegap开发ios应用程序.因为它需要实现人脸识别,所以在这里我也使用本地语言Objective-C.
I am developing an ios application by using phonegap. Because it needs to implement face recognition, so I also use native language objective-c here.
在主故事板上,我有两个视图控制器.一个叫做"FaceRec",它是完全使用objective-c创建的.人脸识别功能在此实现.另一个是phonegap主视图控制器.它们通过使用Tab Bar Controller连接.
In main story board, I have two view controllers. One is called "FaceRec", it is totally created by using objective-c. The face recognition functionality is implemented here. The other one is phonegap main view controller. They are connected by using Tab Bar Controller.
问题是当用户从Tab Bar Controller进入FaceRec页面并从服务器获得响应时.如果响应为真,我不知道如何将响应从FaceRec发送到phonegap主视图控制器.因为它们在不同的视图控制器中,而且我也不知道如何从Objective-C调用javascript函数.
The problem is when user enter FaceRec page from Tab Bar Controller and get response from server. If the response is true, I don't know how to send that response from FaceRec to phonegap main view controller. Because they are in different view controller and I also don't know how to call javascript function from objective-c.
我知道通过使用插件,我可以从javascript调用Objective-c方法并获得响应,但是我想要的是通过不同的视图控制器将Objective-c中的数据发送到phonegap并调用javascript函数.
I know by using plugin I can call objective-c method from javascript and also get response, but what I want is to send data from objective-c to phonegap through different view controller and call a javascript function.
反正有什么主意吗?预先感谢!
Does anyway has any ideas? Thanks in advance!
这是index.html中的代码:
Here is the code in index.html:
<script type="text/javascript">
function login()
{
$(function()
{
$.mobile.navigate( "#main", { transition : "slide", info: "info about the #bar hash" });
});
}
</script>
<div id="loginPage" data-role="page" data-theme="b">;
<div id="main" data-role="page" data-theme="b">
推荐答案
要使用以下方法从Obj-C调用Javascript
函数:
To call Javascript
function from Obj-C using something like:
[self writeJavascript:@"YourJSFunction();"];
您还可以将任何内容作为字符串传递给JS
:
You can also pass anything as a string to JS
:
NSString *js = @"YourJSFunction('";
js = [js stringByAppendingString:yourData];
js = [js stringByAppendingString:@"');"];
[self writeJavascript:js];
yourData
必须为字符串.您可以对几乎所有数组或字典进行JSON序列化,然后将其传递给javascript函数.
Where yourData
has to be a string. You can JSON Serialize basically almost any arrays or dictionaries to then pass it to your javascript function.
这篇关于目标C将数据发送到phonegap并调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!