Difference between the legacy UIAlertView
and the new UIAlertController
is that the latter needs to be presented onto a specific viewcontroller with presentViewController:animated:completion:
. This poses an awkward problem for my use case: what if there is already an UIAlertController
showing (e.g. a rating dialog) when a second viewcontroller gets presented (e.g. an error dialog due to failed network connection). I have experienced that in this case the second UIAlertController
just does not show.
Edit: At the moment I try to show an alert, I do not know if there currently is anything presenting.
How do you cope with this situation?
I found a workaround to find out which viewcontroller I can present the alert upon. I also posted the answer here:
Since
UIAlertController
is itself aUIViewController
, you can present a secondUIAlertController
on top of the first one by presenting from the existing one:Here's a solution I use in Swift 3. It is a function that shows an alert to the user, and if you call it multiple times before the user has dismissed the alert, it will add the new alert text to the alert that's already being presented. If some other view is being presented, the alert will not appear. Not all will agree with that behavior, but it works well for simple situations.
This code fulfilling the requirement when app has to present some alert on window and before present its checking that if there is any other AlertController presented already, if presented then present the alert on appeared Alertcontroller otherwise present it on window.
Here is one more alternative, you can optimize it according to your requirement.
This is what I'm using. this way if alert is already display, I prefer the user to dismissed it and not the app. So, incase the view is already presenting alert, I just wait 5 seconds and try again.
I just want to add, I did not test this too much, but it works.(from 1 test I made), so I hope I'm not missing something, cause I thought about this problem for long time, and this solution sound too easy :)