Library / Outcomes & notifications
Success (task completed)
A user-initiated task finishes: form submitted, file saved, order placed.
import SwiftUI
struct SaveButton: View {
@State private var saved = false
var body: some View {
Button {
Task {
try? await save()
withAnimation { saved = true }
}
} label: {
Label(saved ? "Saved" : "Save",
systemImage: saved ? "checkmark" : "square.and.arrow.down")
}
.sensoryFeedback(.success, trigger: saved) { _, new in new }
}
func save() async throws { try await Task.sleep(for: .seconds(0.5)) }
} let haptic = UINotificationFeedbackGenerator()
func submitForm() {
haptic.prepare() // before the async work finishes
api.submit { result in
DispatchQueue.main.async {
haptic.notificationOccurred(.success)
// show confirmation UI at the same instant
}
}
}