Simple and elegant way to handle UIAlertController.

Overview

SwiftyAlert

SwiftAlert is simple and elegant way to handle UIAlertController.

Feature

  • Handle action with async/await
  • Method chain
  • Support UITextField
  • Support text based actions enable or disable
  • Support Popover

Requirements

  • Swift 5.7+
  • iOS 13.0+
  • tvOS 13.0+

Usage

Basic Alert

The basic is build an alert with method chain.

let alert = Alert(style: .alert)
  .title("Title")
  .message("Message")
  .action(.default("Done"))

_ = await alert.present(from: vc)

Handle Action

When closed alert, result is .done or .cancel. .done contains an item passed in from the action. If passed no item, the type is Void.

let alert = Alert(style: .alert)
  .title("Delete")
  .message("Are you sure?")
  .action(.cancel("Cancel"))
  .action(.destructive("Delete")) // destructive style

switch await alert.present(from: vc) {
case .done:
  print("Delete")
case .cancel:
  print("Cancel")
}

Action Sheet (Multiple Actions)

This is an action sheet that passes multiple string items. .done has a selected item.

When providing an action sheet on the iPad, you can set a popover presentation controller with popoverPresentation method.

let sheet = Alert(style: .actionSheet)
  .title("Select")
  .message("Select a fruit")
  .action(.cancel("Cancel"))
  .action(.default("Apple", item: "🍎"))
  .action(.default("Grapes", item: "🍇"))
  .action(.default("Peach", item: "🍑"))
  .popoverPresentation { popover in // for ipad
    popover.sourceView = sourceView
    popover.sourceRect = sourceRect
  }

switch await sheet.present(from: vc) {
case .done(let item):
  print("Select \(item)") // item is [🍎, 🍇, 🍑]
case .cancel:
  print("Cancel")
}

Alert with a text field

You can add and configure text fields using the addTextField method.

When a text field is added, .done has an item and texts.

let alert = Alert(style: .alert)
  .title("Name")
  .addTextField {
    // Configure text field
    $0.placeholder = "Enter a name"
  }
  .action(.cancel("Cancel"))
  .action(.default("Done"))

switch await alert.present(from: vc) {
case .done(_, let texts):
  print("Name is \(texts[0])")
case .cancel:
  print("Cancel")
}

Alert with multiple text fields

You can use textChanged method for the buttons enable or disable dependng on text input.

let alert = Alert(style: .alert)
  .title("Login")
  .message("Login to service")
  .addTextField {
    $0.placeholder = "Account"
  }
  .addTextField {
    $0.placeholder = "Password"
    $0.isSecureTextEntry = true
  }
  .textChanged { texts, actions in
    let account = texts[0]
    let password = texts[1]
    for action in actions where action.style != .cancel {
      action.isEnabled = !id.isEmpty && !password.isEmpty
    }
  }
  .action(.cancel("Cancel"))
  .action(.default("Connect"))

switch await alert.present(from: vc) {
case .done(_, let texts):
  print("account: \(texts[0]), password: \(texts[1])")
case .cancel:
  print("Cancel")
}

License

MIT license. See LICENSE for details.

You might also like...
Easy Swift UIAlertController
Easy Swift UIAlertController

EZAlertController Easy Swift UIAlertController One line setup for all UIAlertControllers Button action with closures instead of selectors Easily custo

A customizable, full-feature, lightweight iOS framework to be used instead of UIAlertController.
A customizable, full-feature, lightweight iOS framework to be used instead of UIAlertController.

A customizable, full-feature, lightweight iOS framework to be used instead of UIAlertController.

This is an iOS control for selecting a date using UIDatePicker in an UIAlertController like manner
This is an iOS control for selecting a date using UIDatePicker in an UIAlertController like manner

RMDateSelectionViewController This framework allows you to select a date by presenting an action sheet. In addition, it allows you to add actions arro

This is an iOS control for presenting any UIView in an UIAlertController like manner
This is an iOS control for presenting any UIView in an UIAlertController like manner

RMActionController This framework allows you to present just any view as an action sheet. In addition, it allows you to add actions around the present

This is an iOS control for selecting something using UIPickerView in an UIAlertController like manner
This is an iOS control for selecting something using UIPickerView in an UIAlertController like manner

RMPickerViewController This framework allows you to pick something with a picker presented as an action sheet. In addition, it allows you to add actio

An easy to use UIAlertController builder for swift

LKAlertController An easy to use UIAlertController builder for swift Features Short and simple syntax for creating both Alerts and ActionSheets from U

Customizable replacement for UIAlertController

ActionSheet Customizable replacement for UIAlertController. Requirements Installation Swift Package Manager The Swift Package Manager is a tool for au

UIAlertController with continuity.

CuckooAlert Allow multiple use of presentViewController to UIAlertController. You may be disappointed from this. Do you imagine that cuckoo spit out s

Use UIAlertController like a boss.
Use UIAlertController like a boss.

Description Requirements Installation CocoaPods Carthage Usage License Description CatAlertController is a high level manager object that with chainin

Owner
Jaesung Jung
Jaesung Jung
A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.

Introduction Popup Dialog is a simple, customizable popup dialog written in Swift. Features Easy to use API with hardly any boilerplate code Convenien

Orderella Ltd. 3.8k Dec 20, 2022
Simple Alert View written in Swift, which can be used as a UIAlertController. (AlertController/AlertView/ActionSheet)

DOAlertController Simple Alert View written in Swift, which can be used as a UIAlertController replacement. It supports from iOS7! It is simple and ea

Daiki Okumura 406 Sep 5, 2022
A simple custom popup dialog view for iOS written in Swift. Replaces UIAlertController alert style.

A simple custom popup dialog view for iOS written in Swift. Replaces UIAlertController alert style.

donggyu 5 Jan 26, 2022
💬 A tiny extension for UIAlertController that makes working with it very simple. Only 150 lines of code.

AlertController ?? A tiny extension for UIAlertController that makes working with it very simple. Only 150 lines of code. Alert let alert = UIAlertCon

Mezhevikin Alexey 9 Nov 2, 2022
Advanced usage of UIAlertController and pickers based on it: Telegram, Contacts, Location, PhotoLibrary, Country, Phone Code, Currency, Date...

Alerts & Pickers Advanced usage of native UIAlertController with TextField, TextView, DatePicker, PickerView, TableView, CollectionView and MapView. F

RV 5.5k Dec 22, 2022
PMAlertController is a great and customizable alert that can substitute UIAlertController

PMAlertController is a small library that allows you to substitute Apple's uncustomizable UIAlertController, with a beautiful and totally customizable

Paolo Musolino 2.5k Jan 3, 2023
PMAlertController is a great and customizable alert that can substitute UIAlertController

PMAlertController is a small library that allows you to substitute Apple's uncustomizable UIAlertController, with a beautiful and totally customizable

Paolo Musolino 2.5k Jan 3, 2023
Advanced usage of UIAlertController and pickers based on it: Telegram, Contacts, Location, PhotoLibrary, Country, Phone Code, Currency, Date...

Alerts & Pickers Advanced usage of native UIAlertController with TextField, TextView, DatePicker, PickerView, TableView, CollectionView and MapView. F

RV 5.5k Dec 26, 2022
Swifty, modern UIAlertController wrapper.

Alertift Alertift.alert(title: "Alertift", message: "Alertift is swifty, modern, and awesome UIAlertController wrapper.") .action(.default("❤️"))

Suguru Kishimoto 287 Jan 7, 2023
An easier constructor for UIAlertController. Present an alert from anywhere.

ALRT An easier constructor for UIAlertController. Present an alert from anywhere like this. ALRT.create(.alert, title: "Alert?").addOK().addCancel().s

Masahiro Watanabe 97 Nov 11, 2022