问题
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.
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.
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.
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!
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">
回答1:
To call Javascript
function from Obj-C using something like:
[self writeJavascript:@"YourJSFunction();"];
You can also pass anything as a string to JS
:
NSString *js = @"YourJSFunction('";
js = [js stringByAppendingString:yourData];
js = [js stringByAppendingString:@"');"];
[self writeJavascript:js];
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.
来源:https://stackoverflow.com/questions/25850277/objective-c-send-data-to-phonegap-and-call-javascript-function