Phonegap Cordova call javascript functions from Objective-C

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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 :)

回答1:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!