All Products
Search
Document Center

Mobile Platform as a Service:Toast component

Last Updated:Jul 19, 2023

AUToast defines Toast controls for mPaaS. AUToast is developed from APToast of APCommonUI. Use AUToast instead of APToast.

This component contains two types of Toast controls:

  • Common Toast

  • Modal Toast

The modal Toast has a transparent background layer, but the common Toast does not. Users cannot click the area covered by the background layer.

API description

// The declaration of the log output function, which is set by the external system.
typedef void(*AUToastLogFunc)(NSString *tag, NSString *format, ...);
extern AUToastLogFunc g_ToastExternLogFunc; // The global variables of the log output function are set by external system.
#define AUToastLog(fmt, ...) {if(g_ToastExternLogFunc)g_ToastExternLogFunc(@"@AUToast",fmt,##__VA_ARGS__);}


#define AUToast_Default_Duration 2.0    // AUToast Default display duration.
#define AUToast_Strong_Duration 1.5     // AUToast Display duration of the strong prompt.
#define AUToast_Weak_Duration 1.0       // AUToast Display duration of the weak prompt.

/**
* Add a new toast icon to the end of the existing icons instead of in the middle. Otherwise, an error will occurs in business use.
*/
typedef enum{
AUToastIconNone = 0,    // No icon.
AUToastIconSuccess,     // The success icon.
AUToastIconFailure,     // The failure icon.
AUToastIconLoading,     // The loading icon.
AUToastIconNetFailure,  // Network failure.
AUToastIconSecurityScan,// Security scanning.
AUToastIconNetError,    // The network error causing connection failure.
AUToastIconProgress,    // The loading icon indicating the loading progress.
AUToastIconAlert,       // The alarm icon.
} AUToastIcon;

/**
* The Toast control.
*/
@interface AUToast : UIView

@property (nonatomic, assign) CGFloat xOffset; // Set the offset to the central point of the parent view in the x-axis.
@property (nonatomic, assign) CGFloat yOffset; // Set the offset to the central point of the parent view in the y-axis.

/*
* The modal display prompt displayed in the key window. The system does not respond to user operations.
* Call the dismissToast method to hide the Toast. 
*
* @param text The displayed text. Default value: loading.
* @param logTag The log ID.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentToastWithText:(NSString *)text
logTag:(NSString*)logTag;

/**
* Show the Toast. To hide the Toast, call the dismissToast method.
*
* @param superview The parent view.
* @param text      Displayed text.
* @param logTag    The log tag.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentToastWithin:(UIView *)superview
text:(NSString *)text
logTag:(NSString*)logTag;

/**
* Show the Toast. To hide the Toast, call the dismissToast method.
*
* @param superview The parent view.
* @param icon      The icon type.
* @param text      Displayed text.
* @param logTag    The log tag.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentToastWithin:(UIView *)superview
withIcon:(AUToastIcon)icon
text:(NSString *)text
logTag:(NSString*)logTag;

/**
* Show the Toast.
*
* @param superview The parent view.
* @param icon      The icon type.
* @param text      Displayed text.
* @param duration The display duration.
* @param logTag    The log tag.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentToastWithin:(UIView *)superview
withIcon:(AUToastIcon)icon
text:(NSString *)text
duration:(NSTimeInterval)duration
logTag:(NSString*)logTag;



/**
* Show the Toast.
*
* @param superview     In which view of Toast is displayed.
* @param icon          The icon type.
* @param text          Displayed text.
* @param duration      Display duration.
* @param logTag        The log tag.
* @param completion   Callback after Toast automatically disappears.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentToastWithin:(UIView *)superview
withIcon:(AUToastIcon)icon
text:(NSString *)text
duration:(NSTimeInterval)duration
logTag:(NSString*)logTag
completion:(void (^)())completion;


/**
* Show the Toast.
*
* @param superview     In which the view of Toast is displayed.
* @param icon          The icon type.
* @param text          Displayed text.
* @param duration      Display duration.
* @param delay         Display delay duration.
* @param logTag        The log tag.
* @param completion    Callback after Toast automatically disappears.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentToastWithin:(UIView *)superview
withIcon:(AUToastIcon)icon
text:(NSString *)text
duration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
logTag:(NSString*)logTag
completion:(void (^)())completion;



/*
* Show the modal Toast. To hide the Toast, call the dismissToast method.
* Different from the common Toast, the modal Toast has a transparent background layer, which prevents users from clicking the screen.
*
* @param superview The parent view.
* @param text      Displayed text.
* @param logTag    The log tag.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentModelToastWithin:(UIView *)superview
text:(NSString *)text
logTag:(NSString*)logTag;


/**
* Show the modal Toast.
* Different from the common Toast, the modal Toast has a transparent background layer, which prevents users from clicking the screen.
*
* @param superview     In which view of Toast is displayed.
* @param icon          The icon type.
* @param text          Displayed text.
* @param duration      Display duration.
* @param logTag        The log tag.
* @param completion    Callback after Toast automatically disappears.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentModalToastWithin:(UIView *)superview
withIcon:(AUToastIcon)icon
text:(NSString *)text
duration:(NSTimeInterval)duration
logTag:(NSString*)logTag
completion:(void (^)())completion;


/**
* Show the modal Toast.
* Different from the common Toast, the modal Toast has a transparent background layer, which prevents users from clicking the screen.
*
* @param superview     In which the view of Toast is displayed.
* @param icon          Icon type.
* @param text          Displayed text.
* @param duration      Display duration.
* @param delay         Display delay duration.
* @param logTag        The log tag.
* @param completion    Callback after Toast automatically disappears.
*
* @return The displayed Toast object.
*/
+ (AUToast *)presentModalToastWithin:(UIView *)superview
withIcon:(AUToastIcon)icon
text:(NSString *)text
duration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
logTag:(NSString*)logTag
completion:(void (^)())completion;


/*
* Hide the Toast.
*/
- (void)dismissToast;

/**
* Set the prefix text of the progress. If this parameter is not set, the default value is "Loading data".
* The setting is effective only when the Toast type is AUToastIconProgress. Otherwise, ignore this parameter.
*
*  @param prefix The text.
*/
- (void)setProgressPrefix:(NSString*)prefix;

/**
* Show the data loading progress in percentage.
* The setting is effective only when the Toast type is AUToastIconProgress. Otherwise, ignore this parameter.
*
* @param value      Currently loaded data. The value range is 0.0 to 1.0.
*
*/
- (void)setProgressText:(float)value;

@end

Sample code

[AUToast presentToastWithin:self.view withIcon:AUToastIconNetFailure text:@"System Busy" logTag:@"demo"];
[AUToast presentToastWithin:self.view withIcon:AUToastIconSuccess text:@"Success" logTag:@"demo"];
[AUToast presentToastWithin:self.view withIcon:AUToastIconFailure text:@"Failure" logTag:@"demo"];
[AUToast presentToastWithin:self.view withIcon:AUToastIconAlert text:@"Alarm" logTag:@"demo"];

// Loading.
[AUToast presentToastWithin:self.view withIcon:AUToastIconLoading text:nil logTag:@"demo"];

// Set the scenario where the progress appears.
AUToast *toast = [AUToast presentToastWithin:self.view withIcon:AUToastIconProgress text:@"Loading" logTag:@"demo"];
toast.origin = point;
[toast setProgressPrefix:@"~~~"];
[toast setProgressText:0.5];

// The modal Toast.
[AUToast presentModalToastWithin:weakSelf.view withIcon:AUToastIconLoading text:@"Modal toast,There are so many longest texts,is too long" duration:3 logTag:@"demo" completion:NULL];
[AUToast presentModalToastWithin:weakSelf.view withIcon:AUToastIconLoading text:@"Modal toast,There are so many longest texts,is too long" duration:3 delay:2  logTag:@"demo" completion:NULL];