Listen for the navigation bar click event

This topic guides you through how to listen for the mini program navigation bar click event.

From IAPConnect 2.34.0, Griver supports the click event callbacks when clicking items on the navigation bar by implementing the GRVNavigationBarEventDelegate protocol.

Procedures

Step 1. Implement the GRVNavigationBarEventDelegate protocol

Implement GRVNavigationBarEventDelegate protocol and write code in the required methods according to your business logic.

Code sample:

copy
class DemoNavigationBarEventDelegate: NSObject, GRVNavigationBarEventDelegate {
    func navigationBarItemDidClicked(with type: GRVNavigationBarItemClickedType){
        // navigation bar item was clicked
    }

    func h5NavigationBarBackItemDidClicked(withUrl url: String?, completionHandler: @escaping (Bool) -> Void) {
        // H5 page, return event interception
    }
}

Step 2. Register the GRVNavigationBarEventDelegate protocol

Refer to the following code sample. Configure the GRVExtensionDelegate to register the navigation bar click event (GRVNavigationBarEventDelegate) after initializing the SDK.

copy
let extensionDelegate = GRVExtensionDelegate()
extensionDelegate.navigationBarEventDelegate = DemoNavigationBarEventDelegate()

Protocol

GRVNavigationBarEventDelegate protocol

copy
/**
 * @brief Enumerates the types of actions triggered by clicks on navigation bar items.
 */
typedef NS_ENUM(NSUInteger, GRVNavigationBarItemClickedType) {
    /**
     * @brief Unknown action type (default value).
     */
    GRVNavigationBarItemClickedTypeUnknown = 0,

    /**
     * @brief User clicked the 'Back' button to navigate to the previous page.
     */
    GRVNavigationBarItemClickedTypeBack,

    /**
     * @brief User clicked the 'Close' button to close the current mini program.
     */
    GRVNavigationBarItemClickedTypeCloseApp,

    /**
     * @brief User clicked to display the options menu panel.
     */
    GRVNavigationBarItemClickedTypeOptionMenu,
};


@protocol GRVNavigationBarEventDelegate <NSObject>

@optional

// navigation bar item did clicked
- (void)navigationBarItemDidClickedWithType:(GRVNavigationBarItemClickedType)type;


/// Implementing this protocol will intercept the return event of webview.
/// - Parameters:
///   - url: The URL of the current page
///   - completionHandler: false:Return to the previous page
///                        true: Do not return to the previous page
- (void)h5NavigationBarBackItemDidClickedWithUrl:(NSString * _Nullable)url completionHandler:(void(^)(BOOL))completionHandler;


@end

Method

navigationBarItemDidClickedWithType:

Parameters

Field

Data type

Required

Description

type

GRVNavigationBarItemClickedType

Yes

The type of navigation bar item that is clicked.

Valid values:

  • GRVNavigationBarItemClickedTypeUnknown: Indicates that the user clicks a UI element that can't be identified by the super app.
  • GRVNavigationBarItemClickedTypeBack: Indicates that the user clicks a UI element on the navigation bar to return to the previous page.
  • GRVNavigationBarItemClickedTypeCloseApp: Indicates that the user clicks a UI element on the navigation bar to close the current mini program.
  • GRVNavigationBarItemClickedTypeOptionMenu: Indicates that the user clicks a UI element on the navigation bar to show the option menu panel.

h5NavigationBarBackItemDidClickedWithUrl:completionHandler:

Parameters

Field

Data type

Required

Description

url

NSString

No

Indicates the H5 page URL.

completionHandler

Function

Yes

Callback of the interception result.