I'm using MPMoviePlayerController in iOS. I'm listening on any errors it may have while playing videos. In my error handlers, I pop up an UIAlertView. Sometimes errors may occur in quick succession of each other and thus multiple alert boxes will stack up. For a better user experience, I wish to not pop up another alert if an earlier one is still displayed.
6 Answers
Try this:
Set a boolean to true
when you pop up an alert, set it to false
when you close an alert, and always check the boolean to see if it's true
before you pop up an event. If it is true
, you'll know you have an alert window already showing.
-
Absolutely not. The code you pasted is just as bad as using SPI, and while it probably won't get you rejected (because it's hard to find subview poking like you're doing), it's something that rightfully *should* get you rejected from the app store. It's also making assumptions about how UIAlertView works and could possibly break on an OS update. – Lily Ballard Aug 17 '11 at 21:12
-
Thanks, I've removed that part of my answer. – mopsled Aug 17 '11 at 21:16
When an alert appears, it will be moved to a _UIAlertOverlayWindow. Therefore, a pretty fragile method is to iterate through all windows and check if there's any UIAlertView subviews.
-(BOOL)checkAlertViewVisibleStatus
{
for (UIWindow* window in [UIApplication sharedApplication].windows)
{
NSArray* subviews = window.subviews;
if ([subviews count] > 0)
if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
return YES;
}
return NO;
}
This is undocumented as it depends on internal view hierarchy, although Apple cannot complain about this. A more reliable but even more undocumented method is to check if `
[_UIAlertManager visibleAlert]
` is nil.
These methods can't check if a UIAlertView from SpringBoard is shown.

- 1,040
- 8
- 20
You can implement this yourself trivially. Since you're displaying the alert, and you're also the alert's delegate so you know when it's gone, you can easily track whether there's an alert visible just by setting a boolean flag upon alert show and alert hide. That way if the boolean is set, you can quash any subsequent alerts.

- 182,031
- 33
- 381
- 347
Use the new UIAlertViewController. If you try to present an alert while another is in view it ignores it and outputs the warning shown below. It's a nasty side affect for people who want the traditional behaviour of stacked alerts but for your case it's a good solution.
Warning: Attempt to present <UIAlertController: 0x7f9ef34c17e0> on <MasterViewController: 0x7f9ef344ec90> which is already presenting (null)

- 26,330
- 7
- 115
- 133
It should work:
-(BOOL) doesAlertViewExist
{
if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
{
return NO;//AlertView does not exist on current window
}
return YES;//AlertView exist on current window
}

- 20,788
- 5
- 31
- 43
As far as I know, the only way is to keep track of whether or not an alert is currently being displayed and/or one is currently being dismissed within your application. Try showing the alert in the appDelegate, and then using a notification to notify the appDelegate each time the alert is closed. This way the appDelegate keeps track of whether or not there is an alert with a boolean flag variable.

- 1,149
- 8
- 14