目录

ArkWeb_WebMessagePortAPI

typedef struct {...} ArkWeb_WebMessagePortAPI

Overview

ArkWeb_WebMessagePortAPI is a native API struct for web message ports. This struct provides functions such as message port creation, closing, message sending, and message receiving callback registration. This API is a core component of the postMessage bridge, supporting the establishment of persistent bidirectional communication channels between native code and web pages. It is suitable for scenarios where data interaction between native apps and web pages is required, solving cross-language communication challenges and improving app extensibility and development efficiency.

Web message port related APIs must be called on the UI thread by using the OH_ArkWeb_GetNativeAPI method. Before calling, you are advised to use ARKWEB_MEMBER_MISSING to check the availability of function pointers, preventing crashes caused by mismatches between the SDK and the device ROM.

Since: 12

Related module: Web

Header file: arkweb_type.h

Summary

[h2]Member Variables

Name Description
size_t size Size of the struct, in bytes.

[h2]Member Functions

Member Function Description

[h2]postMessage()

ArkWeb_ErrorCode (*postMessage)(const ArkWeb_WebMessagePortPtr webMessagePort, const char* webTag, const ArkWeb_WebMessagePtr webMessage)

Description

Sends a message to the HTML page. It is used when native code needs to pass data, instructions, or configuration information to a web page, for example, form data synchronization and control command delivery.

Parameters

Name Description
const ArkWeb_WebMessagePortPtr webMessagePort Pointer to the message port.
const char* webTag Name of the Web component, used to identify the Web component to operate. It must be a unique identifier bound to the Web component. If no Web component bound to webTag is found, an initialization failure error is returned.
const ArkWeb_WebMessagePtr webMessage Message to send.

Returns

Type Description
ArkWeb_ErrorCode

Result code.

ARKWEB_SUCCESS: execution successful.

ARKWEB_INVALID_PARAM: invalid parameter.

Possible causes:

- webMessagePort or webMessage is null.

- The parameter type is incorrect.

Solutions:

- Check whether the parameter is a null pointer.

- Verify that the parameter type meets the API requirements.

ARKWEB_INIT_ERROR: initialization failed. No Web component bound to webTag is found.

Possible causes:

- The Web component is not properly initialized.

- The webTag parameter does not match the actual Web component name.

Solutions:

- Ensure that the Web component has been initialized.

- Check whether the webTag parameter matches the Web component name.

[h2]close()

void (*close)(const ArkWeb_WebMessagePortPtr webMessagePort, const char* webTag)

Description

Closes a message port.

Parameters

Name Description
const ArkWeb_WebMessagePortPtr webMessagePort Pointer to the message port.
const char* webTag Name of the Web component.

[h2]setMessageEventHandler()

void (*setMessageEventHandler)(const ArkWeb_WebMessagePortPtr webMessagePort, const char* webTag,
        ArkWeb_OnMessageEventHandler messageEventHandler, void* userData)

Description

Sets a callback for receiving HTML messages. It is used when messages, requests, or event notifications from a web page need to be received and processed, for example, receiving user input and status update notifications.

Parameters

Name Description
const ArkWeb_WebMessagePortPtr webMessagePort Pointer to the message port.
const char* webTag Name of the Web component.
ArkWeb_OnMessageEventHandler messageEventHandler Callback used to handle messages.
void* userData User-defined data that is passed to the messageEventHandler callback when triggered. It can be used to carry context information or additional service data, and its lifecycle is managed by the app.