Customize toasts
Toasts are brief notifications that appear on the user interfaces of mini programs to provide feedback about events and actions, such as confirming success, informing about failures, alerting to errors, or offering quick informational updates.
This guide helps super apps customize toasts to ensure a consistent UI experience. Toasts are displayed when mini programs call the my.showToast JSAPI.
Default user experience
The following table shows the UI examples of default toasts.
Success | Fail | Exception | None |
Before you begin
Ensure that the integrated iOS IAPMiniProgram SDK is version 2.65.0 or later. For more information, see SDK release notes.
Procedure
To customize toasts, take the following steps:
Step 1: Implement GRVToastDelegate
Define a class that implements the GRVToastDelegate
protocol. Within the class, use the showToast
method to define a custom toast. Refer to the following code sample for details:
class DemoToastImpl:NSObject, GRVToastDelegate {
func showToast(with param: GRVToastParam, completionHandler: @escaping () -> Void) {
// Implement the logic to specify a custom toast.
}
func hideToast() {
// //Implement the logic to hide a toast.
}
}
For details about GRVToastDelegate
and its defined methods, see GRVToastDelegate
.
Step 2: Configure toastDelegate
After initializing the SDK, create an instance of GRVExtensionDelegate
and assign the implementation (for example, DemoToastImpl
in the sample) to the toastDelegate
property of the delegate's uiProvider
. Refer to the following code sample for details:
let extensionDelegate = GRVExtensionDelegate()
extensionDelegate.uiProvider.toastDelegate = DemoToastImpl()
Protocol
GRVToastDelegate
The following code shows the definition of the GRVToastDelegate
protocol:
@protocol GRVToastDelegate <NSObject>
/// Display a custom toast.
/// - Parameters:
/// - param: Parameter
/// - completionHandler: Completion handler
- (void)showToastWithParam:(GRVToastParam *)param completionHandler:(void(^)(void))completionHandler;
/// Hide a custom toast.
- (void)hideToast;
@end
The protocol defines two methods. The following table lists the details:
Method | Required | Description |
Yes | The method that is called by the SDK to render a custom toast. | |
Yes | The method that is called by the SDK to hide a toast. |
showToastWithParam:completionHandler:
The showToastWithParam:completionHandler:
method has the following input parameters whose values are passed by the SDK:
Parameter | Data type | Required | Description |
param | Yes | An object containing the configuration for the custom toast. | |
completionHandler | Function | Yes | A callback that is triggered when the toast is displayed. |
GRVToastParam
The following code shows the definition of the GRVToastParam
object:
#pragma mark - GRVToastType
typedef NSString * GRVToastType NS_TYPED_EXTENSIBLE_ENUM;
FOUNDATION_EXPORT GRVToastType const GRVToastTypeSuccess;
FOUNDATION_EXPORT GRVToastType const GRVToastTypeFail;
FOUNDATION_EXPORT GRVToastType const GRVToastTypeException;
FOUNDATION_EXPORT GRVToastType const GRVToastTypeNone;
#pragma mark - GRVToastParam
@interface GRVToastParam : NSObject
/// current View Controller
@property (nonatomic, weak, nullable) UIViewController *currentViewController;
/// Toast type
@property (nonatomic, copy) GRVToastType type;
/// Toast content
@property (nonatomic, copy) NSString *content;
/// Displaying duration, in ms, 2000 by default
@property (nonatomic, assign) NSTimeInterval duration;
@end
The object has the following parameters:
Parameter | Data type | Required | Description |
currentViewController | UIViewController | No | A weak reference to the view controller that displays and manages the toast. |
type | GRVToastType | Yes | The toast type. Valid values:
|
content | NSString | Yes | The toast content. |
duration | NSTimeInterval | Yes | The duration for which the toast remains visible, in milliseconds. |
hideToast
The hideToast
method does not have input parameters.