如何使用phonegap实现回调

时间:2010-11-15 20:36:53

标签: iphone cocoa-touch cordova

使用iphone上的phonegap进行一些测试;我有一个插件,返回简单的json数据,如下所示:

NSString* retStr = [[NSString alloc] 
  initWithFormat:@"%@({ code: '%@', image: '%@' });", 
  resultText.text,resultImage.image];                       

[ webView stringByEvaluatingJavaScriptFromString:retStr ];  

我来自JS的电话:

var mydata = PhoneGap.exec("MyModile.myFunction", 'mycallback'); 

function mycallback (data) { alert (data); }

返回时不会产生任何东西。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

// get the callback from the arguments
NSString * jsCallback = [arguments objectAtIndex:0];

// create the string
NSString* retStr = [[NSString alloc] 
    initWithFormat:@"%@({ code: '%@', image: '%@' });", 
                                jsCallback,resultText.text,resultImage.image];  

//execute
[ webView stringByEvaluatingJavaScriptFromString:retStr ]; 

答案 1 :(得分:0)

作为对你对Aaron Saunders的评论的回复:

看看这个wiki:http://wiki.phonegap.com/w/page/36753496/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20iOS

您的原生插件方法应该有一个名为arguments的参数,该参数由调用cordova.exec函数“填充”。