chocoford/SwiftyAlert
a lightweight, multiplatform, easy-to-use package that for swiftUI errors handling.
Platforms
- macOS 12.0+
- iOS 15.0+
- macCatalyst 15.0+
- tvOS 15.0+
- watchOS 8.0+
- visionOS 1.0+
Installation
In an app project or framework, in Xcode:
- Select the menu: File → Swift Packages → Add Package Dependency...
- Enter this URL:
https://github.com/chocoford/SwiftyAlert
Or in a Swift Package, add it to the Package.swift dependencies:
let package = Package(
...
dependencies: [
.package(url: "https://github.com/chocoford/SwiftyAlert.git", from: "1.0.0"),
],
targets: [
.target(
...
dependencies: [
...
"SwiftyAlert"
],
...
)
]
)Usage
Import package
import SwiftyAlertInject environments somewhere first
for example, you can inject environments directly in YourApp.swift
@main
struct YourApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.swiftyAlert() // <-- inject environments here.
}
}
}Use with @Environment
struct ContentView: View {
@Environment(\.alert) private var alert // <-- use with @Environment
var body: some View {
Button {
alert(title: "Alert") {
Button {
...
} label: {
Text("Alert action")
}
} message: {}
// or just use with error
do {...} catch {
alert(error)
}
} label: {
Text("Alert")
}
}
}Use with AlertToast
Also if your project has imported AlertToast, you can use alertToast in the same way!
import AlertToast
import SwiftyAlert
struct ContentView: View {
@Environment(\.alertToast) private var alertToast
var body: some View {
Button {
do {...} catch {
alertToast(error)
}
} label: {
Text("Toggle alertToast")
}
}
}Use with AlertToast and .sheet or .fullscreenCover
You can additionally apply .swiftyAlert(...) to make alertToast displayed in sheet or fullscreenCover.
...
.sheet(isPresented: $showSheet) {
SheetView()
.swiftyAlert() // <-- inject for sheet view to display alertToast
}Trouble shooting
Undefined symbols:...
Just Reset Package Caches and build again. The problem will be gone.
Acknowledgment
elai950 - AlertToast: Create Apple-like alerts & toasts using SwiftUI
Package Metadata
Repository: chocoford/SwiftyAlert
Stars: 6
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md