我正在为 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('错误');"
我希望这是激活插件所需要做的唯一事情。
问题:运行应用程序时,我在控制台上收到 FAILED pluginJSON 错误。
输出:
-[CDVCommandQueue executePending] [第 116 行] FAILED pluginJSON = [ "你好插件 2650437", "你好插件", "原生函数", [ "1", "1", “1” ] ]
我做错了什么,请告诉我。我真的很感谢你的努力。请在这里帮助我。
最佳答案
首先跳出来的是你的插件包名。它应该是您的 iOS 类名称,即“HelloPlugin”。
<param name="ios-package" value="HelloPlugin"/>
引用插件的新方式的目的之一是允许插件名称的灵 active 和向后兼容性,尤其是在 Android 上。示例:
<feature name="HelloPlugin">
<param name="ios-package" value="HelloCDVPlugin"/>
<param name="android-package" value="com.phonegap.plugins.HelloCDVPlugin"/>
</feature>
其中“HelloCDVPlugin”是您的 iOS 类名,“com.phonegap.plugins.HelloCDVPlugin”是您的 Android 类名。
关于ios - 用于 ios 的 PhoneGap 插件返回 FAILED pluginJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18059433/