我正在swift中为iOS编写我的自定义插件,但是我收到以下错误:
ERROR: Method 'initialize:' not defined in Plugin 'RabbitMqPlugin'
我已经搜索过以找到解决方案,但我无法弄清楚问题是什么。
以下是我的代码。
plugin.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-rabbitmq"
version="0.0.1">
<name>RabbitMqPlugin</name>
<js-module src="www/plugin.js" name="RabbitMqPlugin">
<clobbers target="RabbitMqPlugin" />
</js-module>
<!-- ios -->
<dependency id="cordova-plugin-cocoapod-support" />
<dependency id="cordova-plugin-add-swift-support" />
<platform name="ios">
<pods-config ios-min-version="9.0" use-frameworks="true">
</pods-config>
<pod name="RMQClient" />
<config-file target="config.xml" parent="/*">
<feature name="RabbitMqPlugin">
<param name="ios-package" value="CDVDevice"/>
</feature>
</config-file>
<source-file src="src/ios/CDVHttpRequest.swift" />
<source-file src="src/ios/CDVRabbitMq.swift" />
<source-file src="src/ios/CDVRabbitMqPlugin.swift" />
</platform>
</plugin>
www文件夹中的plugin.js 文件:
var exec = require('cordova/exec');
var PLUGIN_NAME = 'RabbitMqPlugin';
var RabbitMqPlugin = {
initialize: function(phrase, cb) {
exec(cb, null, PLUGIN_NAME, 'initialize', [phrase]);
}
};
module.exports = RabbitMqPlugin;
src / ios文件夹中的CDVRabbitMqPlugin.swift 文件:
import Foundation
@objc(RabbitMqPlugin) class RabbitMqPlugin : CDVPlugin {
var mRabbit:rabbitMQ!;
@objc(initialize:)
func initialize(_ command: CDVInvokedUrlCommand){
var pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR
);
let token = command.arguments[0] as! String;
if (token.characters.count > 0) {
mRabbit = rabbitMQ(mToken: token);
pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR
);
}
self.commandDelegate!.send(
pluginResult,
callbackId: command.callbackId
)
}
}
我已经安装了cordova-plugin-add-swift-support模块以支持swift。 我为Android添加了自定义插件离子插件,并成功实现了我的离子项目。
希望你能帮助我。谢谢答案 0 :(得分:2)
你在plugin.xml中的功能是错误的,它应该是
<feature name="RabbitMqPlugin">
<param name="ios-package" value="RabbitMqPlugin"/>
</feature>
此外,如果您没有使用CDVClassname命名类,请不要命名包含以CDV开头的类的文件,不确定它是否会在Swift中失败,但更好地命名具有相同名称的Swift文件类它们包含