A little tip on alerts with MVVM in iOS

It’s not uncommon to have alerts in your app and every developer implements showing alerts in some way. I want to share my approach to showing them when app’s architecture is MVVM.

For binding let’s use Bond framework.

AlertModel

You can also add some extra properties if you need, that model is just for my approach explanation.

AlertBuilder

And again, you can add here your extra logic in builind an alert from a model.

Helpful protocols and extensions

And AlertPresentableViewModel is:

Usage

And its view model like this:

That’s all. Build and run and you will see your alert.
The main benefit of this approach is that you have all alert handling logic in your view model.

GitHub link on the project:

--

--

iOS-Developer from Russia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store