我正在为 iOS 应用程序创建 Phonegap/Cordova(版本 2.9.0)自定义插件。我的步骤如下:
我创建了一个 HelloPlugin.js 文件并将其复制到 www/js/ 文件夹下,其中包含代码:
var HelloPlugin = { callNativeFunction: function (success, fail, resultType) { alert('a'); return Cordova.exec( success, fail, "HelloPlugin", "nativeFunction", ['1']); } };
我在 plugins 文件夹下创建了 HelloPlugin.h 和 HelloPlugin.m 文件,代码:
// .h #import <Cordova/CDVPlugin.h> @interface HelloPlugin : CDVPlugin - (void)nativeFunction:(CDVInvokedUrlCommand*)command; @end // .m #import "HelloPlugin.h" @implementation HelloPlugin - (void)nativeFunction:(CDVInvokedUrlCommand*)command { NSLog(@"Hello, this is a native function called from PhoneGap/Cordova!"); } @end
我在 config.xml 文件中添加了以下代码:
<feature name="HelloPlugin"> <param name="ios-package" value="CDVPlugin"/> </feature>
最后我用以下方式修改了 index.html :
- 添加了脚本参考。()
JS代码添加:
function callNativePlugin(returnSuccess) { HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess ); } function nativePluginResultHandler (result) { alert("SUCCESS: \r\n"+result ); } function nativePluginErrorHandler (error) { alert("ERROR: \r\n"+error ); }
添加了两个按钮并调用了函数:
"callNativePlugin('成功');" “callNativePlugin('error');”
我希望这是我激活插件所需要做的唯一事情。
问题:运行应用程序时,我在控制台上收到 FAILED pluginJSON 错误。
输出 :
-[CDVCommandQueue executePending] [第 116 行] FAILED pluginJSON = [“HelloPlugin2650437”,“HelloPlugin”,“nativeFunction”,[“1”,“1”,“1”]]
我犯了什么错误,请告诉我。我真的很感谢你的努力。请在这里帮助我。