arlg
arlg

Reputation: 1138

Phonegap Cordova call javascript functions from Objective-C

I am facing some Objective-C lackiness of knowledge in my PhoneGap App.

I have a library that I need to implement in Objective-C. This Library has some callbacks that I receive in a delegate Class ( called CCController ) :

[MyLib sharedInstance].delegate = self;

This Class is instanciated in the AppDelegate.m like this :

CCController *myClass = [CCController alloc];
[myClass init];

Then, when my Lib sends events, the functions in my CCController are called. I need, at this point, to call my Javascript functions with a parameter.

How can I implement this ?

I have tried calling a function in AppDelegate.m which contains this :

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

But without success, nothing is called in my JS...

How can I implement this ? Thanks for help :)

Upvotes: 0

Views: 4383

Answers (1)

jcesarmobile
jcesarmobile

Reputation: 53301

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];

Plugin development guide

Upvotes: 7

Related Questions