# Delegate Methods
Some features of Mini-Programs require implementation by the native side to be called, such as forwarding and obtaining user information from the Host-App.
Additionally, some features can be opened up for App developers to customize, such as customizing more panels, custom menu items, etc.
# 1. Set the Delegate Method Implementation Class
First, create an implementation class that inherits from AppletHandler.
class MyAppletHandler extends AppletHandler {
Future<void> appletDidOpen(String appId) async {
print("appletDidOpen appId:$appId");
}
void forwardApplet(Map<String, dynamic> appletInfo) {
print("forwardApplet ---");
}
bool customCapsuleMoreButtonClick(String appId) {
// Return true to indicate customizing the more view; return false to use the default more view
// Note: This delegate method only takes effect on iOS. For Android, refer to the FAQ list.
return true;
}
Future<List<CustomMenu>> getCustomMenus(String appId) {
List<CustomMenu> customMenus = [];
return Future.value(customMenus);
}
Future<void> getMobileNumber(Function(dynamic params) param0) {
// TODO: implement getMobileNumber
throw UnimplementedError();
}
Future<Map<String, dynamic>> getUserInfo() {
// TODO: implement getUserInfo
throw UnimplementedError();
}
Future<void> onCustomMenuClick(String appId, String path, String menuId, String appInfo) {
// TODO: implement onCustomMenuClick
throw UnimplementedError();
}
}
Then, set the implementation object for the delegate methods:
Mop.instance.registerAppletHandler(MyAppletHandler());
# 2. Delegate Methods
# 2.1 Forward / Share
The behavior of the forward button in the more menu needs to be implemented by the app; otherwise, clicking it will have no response. To implement the forward feature, simply implement the following delegate method:
void forwardApplet(Map<String, dynamic> appletInfo)
appletInfo contains Mini-Program information (Mini-Program ID, Mini-Program name, logo, etc.) and page information for forwarding (page path, page parameters).
Usage example:
Future<void> forwardApplet(Map<String, dynamic> map) async {
globalMap = map;
Map<String, dynamic> appletInfo =
Map<String, dynamic>.from(map["appletInfo"]);
print("forwardApplet:$appletInfo");
String appId = appletInfo["appId"];
// Extract other parameters from appletInfo to execute the sharing logic.
}
# 2.2 User Information
This delegate event is triggered when calling the getUserInfo API or clicking a Button with the open-type attribute set to getUserInfo.
/**
The return value can refer to the following format:
{
nickName = "Nickname"
avatarUrl = "Avatar URL"
gender = "Gender"
province = "Province"
city = "City"
country = "Country"
}
*/
Future<Map<String, dynamic>> getUserInfo()
Implementation example:
Future<Map<String, dynamic>> getUserInfo() async {
return {
"nickName": "Nickname",
"avatarUrl": "Avatar",
"gender": "Gender",
"province": "Province",
"city": "City",
"country": "Country",
};
}
# 2.3 Get Mobile Number
When a button control with open-type set to getPhoneNumber is used in the Mini-Program and clicked, this delegate event is triggered.
Future<void> getMobileNumber(Function(dynamic params) param0)
Implementation example:
Future<void> getMobileNumber(
Function(dynamic params) getMobileNumberCallback,
) async {
getMobileNumberCallback.call({"phone": "12345678901"});
return Future.value(null);
}
# 2.4 Capsule ... Button Click Event
You can display your own custom more panel view in this event. Return true to indicate you want to customize the more panel view, where you can display your own design. Return false to use the SDK's default more panel view.
bool customCapsuleMoreButtonClick(String appId);
Example code:
bool customCapsuleMoreButtonClick(String appId) {
// First, display the custom more view
// Then, return true
return true;
}
# 2.5 Custom Menu Items
If you only want to add some custom menu items to the more panel and use the default more view, then you can implement the following delegate method.
/// Get custom menus
Future<List<CustomMenu>> getCustomMenus(String appId);
Implementation example:
@override
Future<List<CustomMenu>> getCustomMenus(String appId) {
List<CustomMenu> customMenus = [
CustomMenu('ShareSinaWeibo', 'weibo_icon', 'Sina Weibo', 'onMiniProgram'),
CustomMenu('ShareQQFirends', 'qq_icon', 'QQ', 'common'),
];
return Future.value(customMenus);
}
# 2.6 Implement Custom Menu Item Click Event
For injected custom menu items, the click event of the menu item can be implemented in the following delegate method:
/// Custom menu click handling
Future<void> onCustomMenuClick(
String appId, String path, String menuId, String appInfo);
Implementation example:
Future<void> onCustomMenuClick(
String appId, String path, String menuId, String appInfo) async {
Applet.instance.onCustomMenuClick(
appId,
path,
menuId,
appInfo,
);
return Future.value(null);
}
# 2.7 Mini-Program Open Delegate Event
When a Mini-Program is opened, the following delegate event is triggered. You can perform tasks like logging in this event.
/// Open Mini-Program
Future<void> appletDidOpen(String appId);
Implementation example:
Future<void> appletDidOpen(String appId) async {
print("appletDidOpen appId:$appId");
// Log the opened Mini-Program ID
}