ios - 无法从 inappbrowser 向 Cordova 应用程序发布消息
问题描述
我正在使用此模块 ( https://github.com/apache/cordova-plugin-inappbrowser ) 在我的 Cordova 应用程序中打开外部链接。但是,postMessage
文档中的示例不起作用。
我需要能够让inappbrowser
实例与父级(的opener
)进行通信。鉴于没有opener
对象inappbrowser
,我查看了 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);
});
解决方案
该文档指向尚未发布的版本。我指出了3.1.0-dev
这个包的版本,实现就像一个魅力。
推荐阅读
- javascript - 当我尝试在 JavaScript 中将十六进制字符串转换为整数时,我得到的数字与在 python 中的数字不同
- android - 使用 Volley 库的 JSON 数组请求解析错误
- arrays - 如何从 Google 表格中的范围动态构建数组?
- python-3.x - 在 Python3 的类中动态装饰一个函数
- html - 代码帮助!我想覆盖一个部分的背景和悬停颜色
- angular - 在“路径”中键入“名称”是 2 个模块声明的一部分:构建 Ionic 4 项目时出错
- python - 更改数据框的列名后出现键错误
- neural-network - 用python重置神经网络中的连接
- javascript - 如何使用 mongoose 更新 mongodb 中的值?
- c++ - 由于二维数组声明,C++ 14 中的运行时错误