Customize title bar favorite/unfavorite button
The super app can customize the appearance of the title bar favorite/unfavorite button to ensure a consistent user interface. This button allows user to add or remove a mini program from their favorites. To customize the button, the super app must implement the GRVTitleBarInfoDelegate
protocol. This topic covers the default user experience, implementation steps, and protocol details.
Default user experience
By default, the title bar favorite/unfavorite button is a star-shaped icon. Refer to the following table for UI examples:
Add to favorites | Remove from favorites |
Before you begin
To customize the user interface for the title bar favorite/unfavorite button, ensure that the integrated iOS IAPMiniProgram SDK is version 2.65.3 or later. For more information, see SDK release notes.
Procedure
To customize the title bar favorite/unfavorite button, take the following two steps:
Step 1: Implement the GRVTitleBarInfoDelegate
protocol
Define a class that implements the GRVTitleBarInfoDelegate
protocol. Within the class, implement the getTitleBarItemInfo
method to customize the title bar favorite/unfavorite button. Refer to the following code for a sample implementation. For more information about the protocol and the method, refer to GRVTitleBarInfoDelegate
.
class DemoTitleBarInfoDelegate: NSObject, GRVTitleBarInfoDelegate {
func getTitleBarItemInfo(_ type:GRVTitleBarItemType, status status:GRVTitleBarItemStatus) -> GRVTitleBarItemInfo {
let titleBarInfo = GRVTitleBarItemInfo.init();
// Customize the button based on the type and status parameters
return titleBarInfo;
}
}
Step 2: Configure titleBarInfoDelegate
After initializing the SDK, create an instance of GRVExtensionDelegate
and assign the implementation (for example, DemoTitleBarInfoDelegate
in the sample) to the titleBarInfoDelegate
property of the delegate's uiProvider
. Refer to the following sample configuration code:
let extensionDelegate = GRVExtensionDelegate()
extensionDelegate.uiProvider.titleBarInfoDelegate = DemoTitleBarInfoDelegate()
Structures
GRVTitleBarInfoDelegate
protocol
TheGRVTitleBarInfoDelegate
protocol defines a method for the super app to customize the title bar favorite/unfavorite button, which the SDK then calls to render the custom button. Refer to the following code for the protocol definition:
@protocol GRVTitleBarInfoDelegate<NSObject>
- (GRVTitleBarItemInfo *)getTitleBarItemInfo:(GRVTitleBarItemType)type status:(GRVTitleBarItemStatus)status;
@end
The following table lists the details of the defined method:
Method | Description |
getTitleBarItemInfo:type:status: | Called by the SDK to obtain and render the custom title bar favorite/unfavorite button. For more information, refer to |
getTitleBarItemInfo:type:status:
method
Parameters
This method has the following input parameters:
Parameter | Data type | Required | Description |
type | GRVTitleBarItemType | Yes | The button type. Valid value is:
For more information, refer to |
status | GRVTitleBarItemStatus | Yes | The state of the favorite/unfavorite button. Valid values are:
For more information, refer to |
GRVTitleBarItemType
typedef NSString *GRVTitleBarItemType NS_TYPED_EXTENSIBLE_ENUM;
FOUNDATION_EXPORT GRVTitleBarItemType const GRVTitleBarItemTypeCollect;
GRVTitleBarItemStatus
typedef NSString *GRVTitleBarItemStatus NS_TYPED_EXTENSIBLE_ENUM;
FOUNDATION_EXPORT GRVTitleBarItemStatus const GRVTitleBarItemStatusCollectOn;
FOUNDATION_EXPORT GRVTitleBarItemStatus const GRVTitleBarItemStatusCollectOff;
Return value
The method returns an instance of the GRVTitleBarItemInfo
object. Refer to the following code for the object definition:
@interface GRVTitleBarItemInfo : NSObject
@property(nonatomic,copy) NSString *iconPath;
@property(nonatomic,copy) NSString *darkIconPath;
@end
The object contains the following properties for the super app to customize the favorite/unfavorite button:
Property | Data type | Required | Description |
iconPath | String | Yes | The icon path for light mode. |
darkIconPath | String | Yes | The icon path for dark mode. |