如何使用自定义cordova插件从ionic1与本机ios swift通信

时间:2018-12-04 05:11:17

标签: ios swift cordova ionic-framework

我正在尝试使用自定义cordova插件从ionic 1到ios swift native进行通信,为此我按照https://moduscreate.com/blog/writing-a-cordova-plugin-in-swift-for-ios/此文档开发了一个插件。但我无法沟通。我得到了:

  

-[CDVCommandQueue executePending] [第142行] FAILED pluginJSON = [“ LiveConnectCordova486334569”,“ LiveConnectCordova”,“ echo”,[“ jai”]]

plugin.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>

plugin.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
     )
   }
  }

1 个答案:

答案 0 :(得分:0)

您需要按照以下说明更新LiveConnectCordova.js文件代码。

var exec = require('cordova/exec');
exports.echo = function (arg0, success, error) {
    exec(success, error, 'LiveConnectCordova', 'echo', [arg0]);
};

当您从离子代码中调用echo方法时。

也可以如下调用插件方法。

window["LiveConnectCordova"].echo(
              param,
              function(res) {
                console.log("Response :", res);
              },
              function(err) {
                console.log("Error :", err);
              }
            );

在您的应用中尝试上述通话。

希望这会有所帮助!