目录

Establishing a Data Channel Between the Application and the Frontend Page

The createWebMessagePorts() API allows you to create message ports to implement communication between the application and frontend page.

In the following example, createWebMessagePorts is used to create two message ports on the application and postMessage() is used to forward one of the message ports to the frontend page so that the application and frontend page can exchange messages with each other over the port. After the port is used or before the WebView object is destroyed, the close API is called to disable the port.

FAQs

[h2]What should I do if the application cannot receive messages sent by the HTML5 page?

Check whether the data type is correct. WebMessage supports the string and ArrayBuffer types.

To pass the object type, use the JSON.stringify method to convert it to the string type. Example:

  function PostMsgToEts(data) {
      if (h5Port) {
        let obj = {name:'exampleName',id:10}
        h5Port.postMessage(JSON.stringify(obj));
      } else {
        console.error('h5Port is null. Please initialize it first.');
      }
  }

[h2]Which is executed first, onControllerAttached or javaScriptOnDocumentStart?

javaScriptOnDocumentStart is executed after onControllerAttached.