All Products
Search
Document Center

SuperApp:Extend uni-app miniapp capabilities

Last Updated:Oct 25, 2024

The capabilities that are described in this topic are specific to uni-app and are used to send and receive events between a host app and a miniapp.

The host app sends an event to the miniapp

API

@protocol EMASMiniAppService <NSObject>

/**
 * Send an event only to a uni-app miniapp
 * @param event The name of the event.
 * @param appId The ID of the miniapp.
 * @param data Data of the NSString or NSDictionary type.
 */
-(void)sendEvent:(NSString *)event appId:(NSString *)appId data:(id __nullable)data;

Sample code:

id<EMASMiniAppService> service = [[EMASServiceManager sharedInstance] serviceForProtocol:@"EMASMiniAppService"];
if (service && miniAppId.length > 0) {
	[service sendEvent:@"NativeEvent" appId:miniAppId data:@{@"msg":@"native message"}];
}

The miniapp receives the event sent by the host app

uni.onNativeEventReceive((event,data)=>{
	console.log ('Message received from the host app:' + event + data);
	this.nativeMsg ='Message received from the host app:' + event + " data: " + data;
})

Parameters

Parameter

Data type

Description

event

String

The name of the event.

data

String or JsonObject

The data that is passed by the host app.

The miniapp sends an event to the host app

Send an event to the host app

uni.sendNativeEvent(event,callback)

Parameters

Parameter

Data type

Description

event

String

The name of the event.

data

String or JsonObject

The data parameters in the event.

callback

Function

The callback method for the host app. The data type can be String or JsonObject based on the implementation of the host app.

Sample code:

// Send an event to the host app.
uni.sendNativeEvent('unimp-event', {
	msg: 'unimp message!!!'
}, ret => {
	this.nativeMsg ='Data returned by the host app:' + ret;
})

The host app receives the event sent by the miniapp

When the event that is received by the host app from the miniapp triggers the block callback of EMASMiniAppService, the host app must set the callback block by using setEventReceiveBlock.

@protocol EMASMiniAppService <NSObject>


/** The callback method that is called when a uni-app miniapp sends an event to the native app.
 * @param EMASUniAppReceiveEventBlock  The event callback block that contains the following parameters:
 * @param event The name of the event.
 * @param appid The ID of the miniapp.
 * @param data Data of the NSString or NSDictionary type.
 * @param callback The callback function that is used to pass data back to the miniapp.
 */
-(void)setEventReceiveBlock:(EMASUniAppReceiveEventBlock)receiveBlock;

EMASUniAppKeepAliveCallback description
/**
 * The callback function that is used to pass data back to the miniapp.
 * result: The callback parameters can be of the NSString or NSDictionary type.
 * keepAlive: If keepAlive is set to YES, the callback method can be called multiple times to pass data back to the miniapp. Otherwise, the callback method is removed after the method is triggered once.
 */
typedef void (^EMASUniAppKeepAliveCallback)(id result, BOOL keepAlive);

EMASUniAppReceiveEventBlock description
/** EMASUniAppReceiveEventBlock   The callback block that is executed when a uni-app miniapp sends an event to the native app.
 * @param event The name of the event.
 * @param appid The ID of the miniapp.
 * @param data Data of the NSString or NSDictionary type.
 * @param callback The callback function that is used to pass data back to the miniapp.
 */
typedef void(^EMASUniAppReceiveEventBlock)(NSString *event, NSString *appId, id __nullable data, EMASUniAppKeepAliveCallback callback);

Sample code:

id<EMASMiniAppService> service = [[EMASServiceManager sharedInstance] serviceForProtocol:@"EMASMiniAppService"];

// The method that is called when the host app receives an event sent by a minapp.
[service setEventReceiveBlock:^(NSString *event, NSString *appId, id  _Nullable data, EMASUniAppKeepAliveCallback callback) {

  NSLog(@"Receive UniMP event: %@ data: %@, appId: %@",event, data, appId);

  // The callback function that is used to pass data back to the miniapp.
  // For information about how to use EMASUniAppKeepAliveCallback, see the relevant description.
  if (callback) {
  	callback(@"native callback message",NO);
  }
}];