UIAlertview style dialogue with activity and success / failure states

Ive created a dialogue view a bit like the old UIAlertView, that also adds an activity indicator, and success / failure states. I find it easier to have this one view that can ask the users permission for an action, then show activity such as network activity, and then give feedback for when that activity has finished, whether with success or failure. If the action has ended in failure, it also has a failure state title and message that are displayed for that case.

Its on Github. To use it take the contents of the AlertControllerVC folder at the root and drop them into your project. I create a property on the UIViewController it will be presented on, ie :

weak var alertConfirmVC: AlertConfirmationViewController?

Incidentally – its a ‘weak’ var because we don’t want our presenting UIViewController to hold on to this property after is has been dismissed – otherwise it will not be free’d from memory.

Heres the use case above :

let alertConfirmVC = AlertConfirmationViewController(nibName: "AlertConfirmationViewController", bundle: nil)
alertConfirmVC.modalPresentationStyle = .overCurrentContext //ensures clear background possible
alertConfirmVC.titleText = NSLocalizedString("Do Action?", comment: "")
alertConfirmVC.messageText = NSLocalizedString("Are you sure you want to do this particular thing?", comment: "")
alertConfirmVC.btn0Text = NSLocalizedString("OK", comment: "")
alertConfirmVC.btn1Text = NSLocalizedString("Cancel", comment: "")
alertConfirmVC.failedTitleText = NSLocalizedString("Task Failed", comment: "")
alertConfirmVC.failedMessageText = NSLocalizedString("We were unable to complete this task", comment: "")
alertConfirmVC.delegate = self
self.present(alertConfirmVC, animated: false, completion: nil)
self.alertConfirmVC = alertConfirmVC

As you can see its a UIViewController, so it is created from a nib, and then presented on top of the current UIViewController. You can have a look at the .xib and change it as you see fit – for eg I have a slightly translucent background to obscure the view it is presented over.

It has four states, and when you change the .mode property on the AlertConfirmationVC, it automatically updates the appearance, using animation to make it slightly smoother. The four states are :

enum AlertConfirmationMode {
    case choose
    case spinning
    case success
    case failure

‘Choose’ is the initial state showing initial messages and buttons. ‘Spinning’ shows the UIActivityIndicator, enlarging the view slightly to fit it in. ‘Success’ shows the green tick, and ‘Failure’ shows the red cross.

It has one delegate method you need to implement to conform to AlertConfirmationDelegate, called buttonClicked. The number of the button (0, 1 or 2) is supplied when it is clicked. My demo implementation was as follows, setting the state to ‘.spinning’ for when ok is clicked, and then performing the action I associate with the ok button :

extension ViewController: AlertConfirmationDelegate {
    func buttonClicked(_ no: Int) {
        if no == 0 {
            //ok clicked
            //set it to spinning and perform action associated with ok
            self.alertConfirmVC?.mode = .spinning
        if no == 1 {
            //cancel clicked
            self.alertConfirmVC?.dismiss(animated: true, completion: nil)

Then finally when my action has completed successfully, I call this method, which displays a green tick where the UIActivityIndicator was (in this case for 2000 milliseconds, ie 2 seconds), then dismisses the AlertControllerVC :

self.alertConfirmVC?.setSuccessThenPauseThenDismiss(milliseconds: 2000)

Leave a Reply

Your email address will not be published. Required fields are marked *