我正在尝试使用自定义cordova插件从ionic 1到ios swift Native进行通信,为此我根据https://moduscreate.com/blog/writing-a-cordova-plugin-in-swift-for-ios/这个文档开发了一个插件。但我无法沟通。我得到:
-[CDVCommandQueue executePending] [Line 142] FAILED pluginJSON = ["LiveConnectCordova486334569","LiveConnectCordova","echo",["jai"]]
插件.xml
<?xml version='1.0' encoding='utf-8'?>
<plugin id="com-fibase-ionic-ios-multivideo" version="0.0.1"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"> .
<name>LiveConnectCordova</name>
<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="LiveConnectCordova">
<param name="ios-package" value="LiveConnectCordova" />
</feature>
</config-file>
<js-module name="LiveConnectCordova" src="www/LiveConnectCordova.js">
<clobbers target="LiveConnectCordova" />
</js-module>
<source-file src="src/ios/LiveConnectCordova.swift" />
<dependency id="cordova-plugin-add-swift-support" version="1.7.2"/>
</platform>
插件.js
var exec = require('cordova/exec');
exports.coolMethod = function (arg0, success, error) {
exec(success, error, 'LiveConnectCordova', 'echo', [arg0]);
};
myswift 类
@objc(LiveConnectCordova) class LiveConnectCordova : CDVPlugin {
func echo(command: CDVInvokedUrlCommand) {
var pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR
)
let msg = command.arguments[0] as? String ?? ""
if msg.characters.count > 0 {
pluginResult = CDVPluginResult(
status: CDVCommandStatus_OK,
messageAs: msg
)
}
self.commandDelegate!.send(
pluginResult,
callbackId: command.callbackId
)
}
}
最佳答案
您需要更新您的 LiveConnectCordova.js
文件代码,如下所述。
var exec = require('cordova/exec');
exports.echo = function (arg0, success, error) {
exec(success, error, 'LiveConnectCordova', 'echo', [arg0]);
};
当您从 Ionic 代码中调用 echo
方法时。
还可以像下面这样调用插件方法。
window["LiveConnectCordova"].echo(
param,
function(res) {
console.log("Response :", res);
},
function(err) {
console.log("Error :", err);
}
);
在您的应用中尝试上述调用。
希望这会有所帮助!
关于ios - 如何使用自定义 cordova 插件从 ionic1 到 native ios swift 进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53606051/