Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Javascript from Objective-C through Cordova plugin

I'm working on an iPad app project that is using Phonegap (cordova 2.0.0).

I know how to write a plugin so I can call a native method from the javascript, but what I really need now is to be able to call a Javascript method from my native code through a plugin. Something like:

- (void) callTestJsFunction
{
    [super writeJavascript:@"testJsFunction();"];
}

that would call a testJsFunction() located in one of my JS files in the www folder. We're using Sencha Touch that handles some logic and I need to call a function from the app.js.

I couldn't find any example of this kind on the web, so first I wonder if it is possible to do such a thing? and if so, how?

like image 358
talnicolas Avatar asked Nov 03 '22 09:11

talnicolas


1 Answers

You have to create a plugin and put the native code there instead using it in the AppDelegate.m.

From the plugin class you can do this:

NSString* jsString = [NSString stringWithFormat:@"myJSFunction(\"%@\");", stringParameter]; [self.webView stringByEvaluatingJavaScriptFromString:jsString];

Phonegap Cordova call javascript functions from Objective-C

like image 93
Ezequiel França Avatar answered Nov 12 '22 09:11

Ezequiel França