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:

copy
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:

copy
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:

copy
@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:

Function name

Description

Required

onAppStart:

This function is called when IAPMiniProgram SDK launches a mini program. For more information, refer to Parameter.

O

onAppExit:

This function is called when IAPMiniProgram SDK terminates a mini program. For more information, refer to Parameter.

O

Parameter

The onAppStart and onAppExit methods take the following parameter, which is passed by the SDK:

Name

Type

Description

Required

app

GRVAppSessionInfo

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:

  • TRUE: The current session is a mini program.
  • FALSE: The current session is not a mini program but an HTML5 page.

Note: If miniProgramAppId is not specified, the current session is not a mini program and this parameter value is set to FALSE.

M

miniProgramVersion

NSString

The mini-program version, which follows the major.minor.patch pattern. For example, "1.0.1".

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 DEBUG.

O

miniProgramScene

NSString

The scene where the mini program operates. This parameter is specified if miniProgramSource is specified. Valid values are:

  • PREVIEW: The mini program is in preview mode on a device. For more information, refer to Preview with Real Machine.
  • DEBUG: The mini program is in debugging mode on a device. For more information, refer to Remote Debugging with Real Machine.
  • INSTALL: The mini program is opened via a temporary testing QR code.

O