Phonegap Cordova从Objective-C调用javascript函数 [英] Phonegap Cordova call javascript functions from Objective-C
问题描述
我在我的PhoneGap应用程序中面临一些Objective-C缺乏的知识。
我有一个库,我需要在Objective-C中实现。这个库有一些回调,我收到一个委托类(称为CCController):
[MyLib sharedInstance] .delegate = self;
这个类在AppDelegate.m中是类似的:
CCController * myClass = [CCController alloc];
。
[myClass init];然后,当我的Lib发送事件时,我的CCController中的函数被调用。
我需要,在这一点上,用一个参数调用我的Javascript函数。
我如何实现这个?
我试过调用AppDelegate.m中的函数,其中包含:
NSString * jsString = [NSString stringWithFormat:@myJSFunction(\%@ \);,stringParameter];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];
但是没有成功,我的JS中没有调用...
如何实现?感谢您的帮助:)
解决方案您必须创建一个插件并将原生代码放在< $ c> AppDelegate.m
从插件类可以这样做:
NSString * jsString = [NSString stringWithFormat:@myJSFunction(\%@ \);,stringParameter];
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
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 :)
解决方案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从Objective-C调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!