ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - mshrwtnb/ALRT: An easier constructor for UIAlertController. Present an alert from anywhere.
An easier constructor for UIAlertController. Present an alert from anywhere. - mshrwtnb/ALRT
Visit Site

GitHub - mshrwtnb/ALRT: An easier constructor for UIAlertController. Present an alert from anywhere.

GitHub - mshrwtnb/ALRT: 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().show()

Table of Contents

Features

  • Chainable UIAlertController Setup Methods
  • Support .alert and .actionSheet UIAlertController.Style
  • Support UITextfield UIAlertAction(.alert only)
  • Returns Result whether an alert is successfully displayed. In other words, Unit Testable.

Requirements

  • Xcode 10.2+
  • Swift 5.0
  • iOS 9.0+

Installation

Carthage

github "mshrwtnb/ALRT" ~> 1.3.7

Cocoapods

pod repo update
pod 'ALRT', '~> 1.3.7'

Usage

Basics

.alert

import ALRT

// Instantiate an .alert-type UIAlertController with OK and Cancel actions. Finally, present the alert by calling `show()`.
ALRT.create(.alert, title: "Title", message: "Message").addOK().addCancel().show()

.actionSheet

// Instantiate an .actionSheet-type UIAlertController.
ALRT.create(.actionSheet, message: "Action Sheet")
    .addAction("Option A")
    .addAction("Option B")
    .addDestructive("Destructive Option")
    .show() 

Action Types

Each action comes with different UIAlertAction.Style.

ALRT.create(.alert, title: "Action Types?")
    .addAction("🏂") // .default if not specified
    .addOK() // .default
    .addCancel("❌") // .cancel
    .addDestructive("💣") // .destructive
    .show()

Custom Title

OK and Cancel actions have default titles in English; "OK" and "Cancel". Here, we're overriding the titles in Japanese.

ALRT.create(.alert, title: "Actions In Japanese?").addOK("オーケー").addCancel("キャンセル").show()

Action Handling

Each action has handler that is called when user taps the action. The closure takes two parameters: UIAlertAction and [UITextField]?. The former is self-explanatory. The latter is present if text field(s) is/are added to the alert.

ALRT.create(.alert, title: "Action Handling")
    .addOK() { action, textFields in
        print("\(action.title!) tapped")
    }
    .show()

Result Handling

show() has a completion handler that takes Result. You can ensure if the alert was shown successfully or not. This is useful for unit tests.

ALRT.create(.alert, title: "Result Handling")
    .addOK()
    .show() { result in
        switch result {
        case .success:
            print("Alert is successfully shown")
        case .failure(let error):
            print("Error occurred. \(error.localizedDescription)")
        }
    }

TextField(s)

Textfield(s) can be added to an alert in an use-case such as login.

enum TextFieldIdentifier: Int {
    case username
    case password
}

ALRT.create(.alert, title: "Enter your credentials")
    // Configure textfield
    .addTextField { textfield in
        textfield.placeholder = "Username"
        textfield.tag = TextFieldIdentifier.username.rawValue
    }
    .addTextField() { textField in
        textField.placeholder = "Password"
        textField.isSecureTextEntry = true
        textField.tag = TextFieldIdentifier.password.rawValue
    }
    // If an user selects "Login", textfields above are retrieved in the trailing closure. Distinguish one from another with a tag or identifier.
    .addAction("Login") { _, textfields in
        for textField in textfields ?? [] {
            if let identifier = TextFieldIdentifier(rawValue: textField.tag) {
                switch identifier {
                case .username:
                    // Extract username
                case .password:
                    // Extract password
                }
            }
        }
    }
    .addCancel()
    .show()

Changing source ViewController to present from

Although ALRT can present an alert anywhere, you might want to specify a source view controller for some reason. This can be done easily by passing a view controller to show().

ALRT.create(.alert, title: "Source?")
    .addOK()
    .show(self) // self = source view controller

Default Configuration

Set default tintColor and titles for OK and Cancel buttons.

ALRT.defaultConfiguration = .init(
    tintColor: UIColor.blue,
    okTitle: "OK👍",
    cancelTitle: "Cancel👎"
)

License

ALRT is released under the MIT license. See LICENSE for details.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory