您应该使用 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];