Platform Bridge (@arkui-x.bridge) [Nut Pie]
Introduction
Platform bridging is used to transfer messages between the client (ArkUI) and the platform (Android or iOS), that is, it is used for two-way data transfer between ArkUI and the platform, the ArkUI side calls the platform's methods, and the platform calls the ArkUI side's methods.
Taking the Android platform as an example, ArkTS and Java do not have the ability to call each other. In order to realize the interaction between ArkTS and Java, ArkTS needs to interact with C++, and C++ then interacts with Java, and vice versa. But for developers, it's like ArkTS and Java interacting directly.
This article introduces how to write customized Android and iOS platform code through platform bridging.
Usage scenarios and capabilities
scenes to be used
Platform bridging is mainly used in scenarios where applications need to reuse platform code, but there is no corresponding cross-platform API (excluding UI related) implementation in OpenHarmony.
Specifically, it can be used in the following scenarios:
1. ArkUI and the platform transmit data in both directions, such as passing JSON data, pictures, etc.;
2. The ArkUI side calls the API of the platform, such as obtaining the battery power on the Android or iOS platform. , reuse third-party libraries on the platform, etc.;
3. The platform calls methods on the ArkUI side, such as reusing JavaScript third-party libraries, etc.
illustrate
Platform bridging supports ArkUI calling Android Java API and iOS Objective-C API.
Data type support
The platform bridge transmits data through JSON format or binary format serialization encoding and decoding, and supports basic data types, array types and structured data. Specific support types are as follows:
JSON format data support type table:
ArkTS | Java | Objective-C |
---|---|---|
string | java.lang.String | NSString |
number(32bit integer) | java.lang.Integer | NSNumber numberWithInt |
number(double) | java.lang.Double | NSNumber numberWithDouble |
boolean | java.lang.Boolean | NSNumber numberWithBool |
null | null | NSNull |
Array |
java.util.ArrayList | NSArray |
Map<string, T> | java.util.HashMap | NSDictionary |
Binary format data support type table:
ArkTS | java type | OC type |
---|---|---|
null | null | NSNull |
boolean | java.lang.Boolean | NSNumber numberWithBool |
number(32bit integer) | java.lang.Integer | NSNumber numberWithInt |
number(double) | java.lang.Double | NSNumber numberWithDouble |
string | java.lang.String | NSString |
ArrayBuffer | java.nio.ByteBuffer | NSData |
Array | java.util.ArrayList | NSArray |
Map<stirng, T> | java.util.HashMap | NSDictionary |
illustrate
S represents string, number, and boolean types, and T represents S and its corresponding array type; the Map type only supports string type keys and is only used for method returns.