APAlertView is a powerful, modern SwiftUI alert and action sheet library. It provides a clean, type-safe API for displaying alerts and action sheets with extensive customization options while maintaining backward compatibility.
- π― Type-safe API with modern Swift features
- π Backward compatibility with existing code
- π¨ Extensive customization options
- β‘ Async/await support for modern concurrency
- ποΈ SOLID principles compliant architecture
- π± iOS 13+ support
- π Multiple button styles (default, destructive, cancel)
- π Convenience methods for common use cases
For adding it as a Swift Package in Xcode 13.0 or later, select File > Add Packages > then add the repository URL:
https://github.com/arvindcs/APAlertView.git
Drag all Swift files from the APAlert/Source/ directory to your project.
- iOS 13+
- Xcode 11+
- Swift 5.0+
import SwiftUI
struct ContentView: View {
@StateObject var alertView = APAlertView()
var body: some View {
Button("Show Alert") {
let configuration = AlertConfiguration(
title: "Success",
message: "Operation completed successfully!",
primaryButton: AlertButton(title: "OK") {
print("OK tapped")
}
)
alertView.showAlert(configuration)
}
.initializeAlert(alertView)
}
}Button("Show Confirmation") {
let configuration = AlertConfiguration(
title: "Confirm Action",
message: "Are you sure you want to proceed?",
primaryButton: AlertButton(title: "Confirm") {
print("Confirmed")
},
secondaryButton: AlertButton(title: "Cancel", style: .cancel) {
print("Cancelled")
}
)
alertView.showAlert(configuration)
}Button("Delete Item") {
let configuration = AlertConfiguration(
title: "Delete Item",
message: "This action cannot be undone.",
primaryButton: AlertButton(title: "Delete", style: .destructive) {
print("Item deleted")
},
secondaryButton: AlertButton(title: "Cancel", style: .cancel) {
print("Deletion cancelled")
}
)
alertView.showAlert(configuration)
}Button("Show Confirmation Dialog") {
let configuration = ConfirmationDialogConfiguration(
title: "Choose Action",
message: "Select an action to perform",
buttons: [
AlertButton(title: "Add Item") {
print("Add selected")
},
AlertButton(title: "Edit Item") {
print("Edit selected")
},
AlertButton(title: "Delete Item", style: .destructive) {
print("Delete selected")
},
AlertButton(title: "Cancel", style: .cancel) {
print("Cancelled")
}
]
)
alertView.showConfirmationDialog(configuration)
}alertView.showPhotoPickerConfirmationDialog(
onCamera: { print("Camera selected") },
onPhotoLibrary: { print("Photo library selected") }
)// Success alert
alertView.showSuccessAlert(message: "Operation completed successfully!")
// Error alert
alertView.showErrorAlert(message: "Something went wrong")// Simple confirmation
alertView.showConfirmationAlert(
title: "Save Changes",
message: "Do you want to save your changes?",
onConfirm: { print("Saved") },
onCancel: { print("Cancelled") }
)
// Destructive confirmation
alertView.showDestructiveAlert(
title: "Delete Account",
message: "This action cannot be undone.",
onDestructive: { print("Account deleted") }
)Task {
let result = await alertView.showConfirmationAsync(
title: "Save Changes",
message: "Do you want to save your changes?"
)
if result {
print("User confirmed")
// Save changes
} else {
print("User cancelled")
}
}Task {
let configuration = AlertConfiguration(
title: "Choose Option",
message: "Select an option",
primaryButton: AlertButton(title: "Option A") {},
secondaryButton: AlertButton(title: "Option B") {}
)
let choice = await alertView.showAlertAsync(configuration)
print("User selected: \(choice ?? "none")")
}The original API is still fully supported:
alertView.showAlertView(
title: "Alert",
message: "Some Message",
primaryCompletion: ("OK", {
print("OK tapped")
})
)alertView.showActionSheet(
title: "ActionSheet",
message: "Message",
primaryCompletion: ("Add", { print("Add") }),
secondaryCompletion: ("Delete", { print("Delete") }),
dismissCompletion: ("Cancel", { print("Cancel") })
).initializeAlert(alertView) // For alerts only
.initializeAlertConfirmationDialog(alertView) // For confirmation dialogs only.initializeAlertSystem(alertView) // For both alerts and confirmation dialogs- No breaking changes - all existing code continues to work
- Optional upgrade - you can gradually adopt the new API
- New features - take advantage of type safety and convenience methods
- Keep existing code as-is
- Gradually replace with new API for new features
- Use convenience methods for common patterns
- Consider async/await for new async operations
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License
Copyright (c) 2022 Arvind Patel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

