如何从iOS Cordova插件调用ionic 1方法

时间:2018-08-06 09:55:09

标签: javascript ios objective-c cordova ionic-framework

我为cordova-plugin应用(iOS平台)创建了一个ionic-1。我想从我的js的{​​{1}}文件的本机iOs类中调用我的离子项目的objective-c文件中创建的方法。

**请帮助找出我该如何实现? **

1 个答案:

答案 0 :(得分:1)

您应使用Cordova channel js native 之间建立事件协议。您可以从 cordova-plugin-inappbrowser 获取示例。

链接的简要说明,

使用javascript代码

导入频道库

var channel = require('cordova/channel');

创建以事件名称命名的频道

function InAppBrowser () {
    this.channels = {
        'loadstart': channel.create('loadstart'),
        'loadstop': channel.create('loadstop'),
        'loaderror': channel.create('loaderror'),
        'exit': channel.create('exit'),
        'customscheme': channel.create('customscheme')
    };
}

添加和删除侦听器功能

InAppBrowser.prototype = {
    addEventListener: function (eventname, f) {
        if (eventname in this.channels) {
            this.channels[eventname].subscribe(f);
        }
    },
    removeEventListener: function (eventname, f) {
        if (eventname in this.channels) {
            this.channels[eventname].unsubscribe(f);
        }
    },
};

在init中注册回调(可选)。您也可以在您的应用中执行此操作。

for (var callbackName in callbacks) {
    iab.addEventListener(callbackName, callbacks[callbackName]);
}

以本机代码

开火事件。

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"type":@"loadstart", @"url":[url absoluteString]}];

[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];

[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
相关问题