【问题标题】:Cannot postMessage from inappbrowser to Cordova application无法从 inappbrowser 向 Cordova 应用程序发布消息
【发布时间】: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


    【解决方案1】:

    文档指向尚未发布的版本。我指出了这个包的3.1.0-dev 版本,实现就像一个魅力。

    【讨论】:

    • 您好 Detuned,您能否解释一下您是如何将其指向开发版本的。我被困在同一个位置。试过你的代码,它没有进入脚本。还需要进行哪些更改。
    • @Detuned 如何从 InAppBrowser 中的 openend url 向您的应用发送消息以激活消息事件监听器?
    • @Detuned - 这个功能现在可以在 master 分支上使用吗? (例如,我不需要从单独的分支中提取?)
    【解决方案2】:

    是的,InAppBrowser 尚未实现 postMessage。在这种情况下,唯一的解决方案是使用 iframe 从您的外部网页接收 postMessage。 This has been discussed earlier 也在这个网站上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-28
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多