ToastManager.swift
Purpose
ToastManager.swift is a lightweight utility class that provides simple toast-like message functionality for iOS using UIAlertController. It enables temporary messages to be shown in the app without requiring any third-party dependencies.
Key Features
Singleton design for global access.
Presents a
UIAlertControllerwith a message.Automatically dismisses the alert after a specified duration.
Design Pattern
static let shared = ToastManager()
private init() {}
Implements the Singleton pattern to ensure a single shared instance throughout the app.
Method: showToast
func showToast(message: String, duration: TimeInterval = 2.0, in viewController: UIViewController?)
message: The string to display in the toast.duration: How long the toast should be shown (defaults to 2 seconds).viewController: The currentUIViewControllerto present from.
Behavior:
Creates a
UIAlertControllerwith no title and the message body.Presents the alert on the provided view controller.
Uses
DispatchQueue.main.asyncAfter(...)to dismiss the alert after the duration.
Example usage:
ToastManager.shared.showToast(message: "Sensor connected", in: self)
Use Cases
Notifying the user when a sensor connects or disconnects.
Alerting of a successful file save or error.
Displaying non-intrusive status updates in test builds.