Listen for mini-program lifecycle events
The super app can listen for the mini-program lifecycle events to perform specific operations when IAPMiniProgram SDK launches or terminates the mini program. To listen for these events, the super app must implement the GRVAppEventDelegate
protocol. This guide covers the necessary implementation procedures and protocol references.
Note: Implementations of lifecycle event listeners do not block mini programs from opening or closing. Therefore, these listeners are not suitable for scenarios where the super app requires the user to complete a specific action before closing a mini program.
Procedures
To listen for mini-program lifecycle events, complete the following steps:
Step 1: Implement GRVAppEventDelegate
In your app project, create a new file and define a class that adopts the GRVAppEventDelegate
protocol. Then, selectively implement the functions that suit your needs. Refer to the following code sample for implementation:
class DemoAppEventDelegate: NSObject, GRVAppEventDelegate {
func onAppStart(_ app: any GRVAppSessionInfo) {
print("onAppStart")
}
func onAppExit(_ app: any GRVAppSessionInfo) {
print("onAppExit")
}
}
Step 2: Configure GRVExtensionDelegate
Before the SDK initialization logic, configure GRVExtensionDelegate
by setting the appEventDelegate
class as extensionDelegate.appEventDelegate
and registering the implemented GRVAppEventDelegate
protocol with the SDK. Refer to the following code sample for configuration:
let extensionDelegate = GRVExtensionDelegate()
extensionDelegate.appEventDelegate = DemoAppEventDelegate()
Protocol
GRVAppEventDelegate
The GRVAppEventDelegate
protocol is used to listen for mini-program lifecycle events. The following code shows the protocol definition:
@protocol GRVAppEventDelegate <NSObject>
@optional
// Called when the mini program starts
- (void)onAppStart:(id<GRVAppSessionInfo>)app;
// Called when the mini program terminates
- (void)onAppExit:(id<GRVAppSessionInfo>)app;
@end
The protocol provides the following functions. The super app must implement either one or both of the two functions:
Parameter
The onAppStart
and onAppExit
methods take the following parameter, which is passed by the SDK:
Name | Type | Description | Required |
app | The mini-program instance. | M |
GRVAppSessionInfo
Name | Type | Description | Required |
enteringURL | NSString | The launch URL when opening IAPMiniProgram SDK. | M |
enteringExtraParams | NSDictionary | The additional launch parameters that are passed when opening IAPMiniProgram SDK. | O |
isMiniProgram | BOOL | Whether the current session is a mini program. Valid values are:
Note: If miniProgramAppId is not specified, the current session is not a mini program and this parameter value is set to | M |
miniProgramVersion | NSString | The mini-program version, which follows the | O |
miniProgramAppId | NSString | The unique ID that is assigned by Mini Program Platform to identify a mini program. | O |
miniProgramSource | NSString | This parameter is specified when the mini program is in the debugging phase. The only valid value is | O |
miniProgramScene | NSString | The scene where the mini program operates. This parameter is specified if miniProgramSource is specified. Valid values are:
| O |