Phonegap Cordova从Objective-C调用javascript函数 [英] Phonegap Cordova call javascript functions from Objective-C

查看:137
本文介绍了Phonegap Cordova从Objective-C调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的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];

Plugin development guide

这篇关于Phonegap Cordova从Objective-C调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆