Easy UIAlertController presentation

Chris Mash
2 min readAug 12, 2021

Something that can often be troublesome in iOS (or tvOS) development is presenting alerts at random times while other things might be happening in your UI.

In iOS 8, Apple deprecated UIAlertView, which didn’t require a specific UIViewController to be presented from, as is required in the UIAlertController that replaced it.

Suddenly app developers needed to specify which UIViewController would be presenting the alert, and with that came the problems of “what if it’s already presenting something” and “what if it’s in the process of presenting/dismissing something”.

If you try and present an alert (or any other UIViewController) in one of those scenarios it will silently (just an error logged to the console) fail to present, which could leave your app in a broken state or, at the very least, the user is left without some information/choice you intended them to have.

I came across a partial solution to some of these problems when taking over an existing test app at work, where it would queue up alerts in the case that something was already being presented. It was interesting but it didn’t fully solve the problem so I started looking for a better approach.

That’s when I came across William Boles’ article on queueing and presenting alerts in their own UIWindow. I took what he’d provided, modified and extended it a bit and turned it into a Swift package which you can checkout on GitHub: AlertPresenter.

It’s a great way to be able to present alerts without worrying about the exact state your app is currently in. Sometimes that’s fine, other times you might need something more involved, so this might not be the right solution for everyone, but for fairly simple apps or apps that are being used to test a framework/library it should be a great help!

--

--

Chris Mash

iOS developer since 2012, previously console games developer at Sony and Activision. Twitter: @CJMash