A Swift package for iOS/tvOS for easy alert presentation

Overview

AlertPresenter

Listed on the Swift Package Index and originally posted on my blog.

It can be fiddly to handle the presentation of alerts on a specific view controller, especially when transition animations or other things currently being presented on that view controller can silently prevent the alert from being presented.

AlertPresenter simplifies the process by removing the need to specify which view controller an alert should be presented on. It will also allow you to queue up alerts so that any that need presenting in close proximity won't get missed.

Platforms supported:

  • iOS 12+
  • tvOS 12+

AlertPresenter can be easily used from both Swift and Objective-C. Though it's based on UIKit it can just as easily be used from SwiftUI.

Alert Presenter might not be suitable for anything more than a simplistic app, as you may need more control over the display of alerts alonside your other navigations. But for a test app or other simplistic apps it can be quite useful.

Credit

This package is based on an article by William Boles, so most credit goes to him for the original solution. I've merely modernised it a bit, supported tvOS and iOS 12, packaged it up and added a few more features and testing.

Adding to your project

Follow Apple's guidance to add the package to your project.

Usage

Basic usage

Simply create an instance of AlertPresenter and enqueue an alert, along with the windowScene it should be presented in (required for xOS 13+ if SceneDelegate or SwiftUI's App is being used).

import AlertPresenter

let alertPresenter = AlertPresenter()

func showAlert() {
    let alert = UIAlertController(title: "title",
                                message: "message",
                                preferredStyle: .alert)
    // also works with .actionSheet or a custom UIViewController alert
    let scene = UIApplication.shared.windows.first?.windowScene
    alertPresenter.enqueue(alert: alert, 
                           windowScene: scene)
}

UIWindow.Level

You can initialise AlertPresenter with a UIWindow.Level if required and can use multiple instances for different priorities of alerts that you may need to show over alerts that are already displaying.

Custom Alerts

To ensure AlertPresenter is informed of a custom alert being dismissed it should dismiss itself like this:

presentingViewController?.dismiss(animated: true)

Popover presentations

On iPad an action sheet needs to be told where on the screen it should appear as it's presented as a popover, rather than a bottom sheet as it's presented on iOS. The default behaviour of AlertPresenter is for a popover to be presented in the center of the screen, with no arrows pointing at its source rect.

You can override the default behaviour by passing a PopoverPresentation when enqueuing the alert:

func showAlert() {
    // The closure to be called when the alert is presented
    let presentation = { [weak self] alert in
        guard let self = self else {
            // If we've been deinitalised before presentation return some default
            // details to present the alert centered on the screen
            return PopoverPresentation(sourceRect: .zero,
                                        delegate: nil)
        }
        
        // Calculate the rect to present the alert from, such as the frame of the button
        // in window coordinates
        let rect = button.superview?.convert(button.frame, to: self.view) ?? .zero
        return PopoverPresentation(sourceRect: rect,
                                    delegate: self)
    }
            
    let scene = view.window?.windowScene
    alertPresenter.enqueue(alert: alert,
                           windowScene: scene,
                           popoverPresentation: popoverPresentation)
}

The delegate parameter is a PopoverDelegate. On iOS this conforms to UIPopoverPresentationControllerDelegate, which can be useful if you want to force your custom alert to be a popover on iPhone:

extension MyClass: PopoverDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        // forces the popover presentation style to be honoured on iPhone
        return .none
    }
}

Test apps

There is a minimal iOS 13+, UIKit example in the SimpleExamples folder.

There are various, more complex, test apps in the TestApps folder that demonstrate custom alerts, iPad action sheet presentation customisation, popover customisation, iOS/tvOS 12 and SwiftUI:

  • iOSExample: A SwiftUI example, for iOS 13+, using App instead of UIApplicationDelegate
  • iOSExample_UIKit: A Swift, UIKit example for iOS 12+, using UIApplicationDelegate and SceneDelegate
  • iOSExample_UIKit_Legacy: A Swift, UIKit example for iOS 12+, using just UIApplicationDelegate
  • iOSExample_ObjC: An ObjC, UIKit example for iOS 12+, using just UIApplicationDelegate
  • tvOSExample: A SwiftUI example for tvOS 13+, using App instead of UIApplicationDelegate

Contributions

If you wish to make any contributions to this project, feel free to make a fork and then submit a pull request back with your proposed changes/additions.

Make sure any changes or additions are covered with unit/UI tests and the test apps are updated as appropriate.

Bear in mind that breaking changes should be avoided where possible!

Breaking changes monitoring

Scripts from my PublicAPIMonitoringExample are included as build phases. They output the public API, in both Swift and ObjC, to files such that can be monitored in commits for the introduction of any breaking changes.

SwiftLint

The project is setup with SwiftLint to check code quality a little. It's configured by the .swiftlint.yml file in the root of the project.

Run a scan with the following command in the root of the project: swiftlint

SwiftLint execution is also included in the automated tests.

Jazzy docs

API documentation can be generated with Jazzy. Whilst this isn't published it's useful for pointing out parts of the API that aren't documented correctly (check the undocumented.json file for any warnings after the documentation is generated), so you can be sure the API documentation available within Xcode will be correct and useful to users. .jazzy.yaml contains the configuration.

Generate the documentation using the following command in the root of the project: jazzy

Jazzy execution is also included in the automated tests.

Running automated tests

Fastlane is used to automate running all the tests in one command (or separately), using the recommended setup with Bundler, more info on the Fastlane site.

In the root of the project run bundle exec fastlane <lane>, with the following lane options:

  • allTests: Run all the tests (unit & UI (iOS and tvOS, multiple OS versions), linter and docs checker)
  • unitTests: Run just the unit tests (iOS and tvOS, latest OS version)
  • allUITests: Run all the UI tests (iOS and tvOS, multiple OS versions)
  • uiTests OS:14.5 scheme:iOSExample device:'iPhone 8': Run the UI tests for the 'iOSExample' test app, on iOS 14.5, with and iPhone 8
  • checkDocs: Run the docs checker (looking for undocumented public APIs)
  • lint: Run the linter (looking for code smells)

NOTE: If you have an M1 Mac then Jazzy (used to generate the docs in the checkDocs and allTests lanes) doesn't work in Terminal unless you've set it to open with Rosetta.

You might also like...
Beautiful animated Alert View. Written in Swift
Beautiful animated Alert View. Written in Swift

SCLAlertView Animated Alert View written in Swift, which can be used as a UIAlertView or UIAlertController replacement. Since UIAlertView is deprecate

Simple Alert View written in Swift, which can be used as a UIAlertController. (AlertController/AlertView/ActionSheet)
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

Customizable simple Alert and simple ActionSheet for Swift
Customizable simple Alert and simple ActionSheet for Swift

SimpleAlert It is simple and easily customizable alert. Can be used as UIAlertController. Appetize's Demo Requirements Swift 5.0 iOS 9.0 or later How

A Simple Customizable Alert With Swift
A Simple Customizable Alert With Swift

SimpleCustomizableAlert trying to make my very first library. Support Title Message (TextView) Image TextField Button Action Example let alert

Swift UI Kit to present clean modal/alert
Swift UI Kit to present clean modal/alert

CleanyModal is a good way to use UI-Customised alerts with ease Features Present some kind of clean alerts (With same API as UIAlertViewController) Ad

(Experimental libraries) Controls interrupt handling, such as alert views, and is compatible with UIKit and Swift UI.
(Experimental libraries) Controls interrupt handling, such as alert views, and is compatible with UIKit and Swift UI.

UIPresentCoordinator Controls interrupt handling, such as alert views, and is compatible with UIKit and Swift UI. This library manages items that are

An easier constructor for UIAlertController. Present an alert from anywhere.
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

PMAlertController is a great and customizable alert that can substitute UIAlertController
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

Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets.
Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets.

SPAlert Popup from Apple Music & Feedback in AppStore. Contains Done, Heart, Error and other presets. Supports Dark Mode. I tried to recreate Apple's

Releases(v1.0.0)
Owner
Chris Mash
Chris Mash
SwiftMessages is a very flexible view and view controller presentation library for iOS.

SwiftMessages Overview SwiftMessages is a very flexible view and view controller presentation library for iOS. Message views and view controllers can

SwiftKick Mobile 6.7k Jan 2, 2023
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
Live animated Alert View for iOS written in Swift

Sweet Alert iOS Beautiful Animated custom Alert View inspired from javascript library SweetAlert. Written in Swift this SweetAlertView can be used in

Sahil 2k Dec 22, 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 colored alert view for your iOS.

日本語 KRAlertController KRAlertController is a beautiful and easy-to-use alert controller for your iOS written by Swift. Requirements iOS 10.0+ Xcode 10

K.R.Impedance 52 Jun 30, 2022
Highly configurable iOS Alert Views with custom content views

NYAlertViewController NYAlertViewController is a replacement for UIAlertController/UIAlertView with support for content views and UI customization. Fe

Nealon Young 609 Nov 20, 2022
Custom alert View to iOS applications

A simple, easy and custom iOS UIAlertView written in Swift Malert came to facilitates custom alert views as UIAlertController. Malert allows you to pe

Vitor Mesquita 480 Oct 29, 2022
Simple DropDown Alert View For Any iOS Projects.

⚠️ DEPRECATED, NO LONGER MAINTAINED JDropDownAlert JDropDownALert Simple DropDown Alert View For Any iOS Projects. Usage Top let alert = JDropDown

WonJo 71 Jul 17, 2022
TextFieldAlert - A SwiftUI alert with text field(s) for iOS 13 and greater.

TextFieldAlert A SwiftUI alert with text field(s) for iOS 13 and greater. As Apple is going to introduce text field(s) as an alert actions in iOS 16,

Piotr Sochalewski 13 Dec 5, 2022
Highly customizable alertview and alert/notification/success/error/alarm popup written in Swift

CDAlertView is highly customizable alert popup written in Swift. Usage is similar to UIAlertController. Screenshots Animations Usage Basic usage witho

Candost Dagdeviren 1.1k Dec 30, 2022