【发布时间】:2019-07-04 17:34:34
【问题描述】:
我正在使用这个模块 (https://github.com/apache/cordova-plugin-inappbrowser) 在我的 Cordova 应用程序中打开外部链接。但是,文档中的 postMessage 示例不起作用。
我需要能够让inappbrowser 实例与父级(opener)进行通信。鉴于inappbrowser 没有opener 对象,我查看了repo 的文档和测试,我无法重现postMessage API 在inappbrowser 实例和主Cordova 应用程序(父)之间进行通信.
这是一个取自此 repo 中的文档/测试的简单示例:
const ref = cordova.InAppBrowser.open('http://www.google.com', '_blank');
ref.addEventListener('loadstop', () => {
console.log('loadstop has been fired'); // this fires
// when this has been executed, `webkit` variable doesn't exist inside of the `inappbrowser`
// instance
ref.executeScript({
code: `(() => {
var message = "TESTING!!!";
webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(message));
})()`
});
});
// this is never fired
ref.addEventListener('message', (...args) => {
console.log('MESSAGE RECEIVED FROM IN_APP_BROWSER', ...args);
});
【问题讨论】:
标签: ios cordova ionic-framework phonegap-plugins hybrid-mobile-app