Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: detecting if a UIAlert/UIActionSheet are open

In my iOS application, I have a timer firing up, and when it fires, I need to be able to detect whether there's an Alert (UIAlertView) or an Action Sheet (UIActionSheet) open.

One way would be to modify the code presenting the alerts/actionsheets - but unfortunately this is not an option in my case.

So, the question is - is there a way of knowing/detecting whether an alert or action sheet have been opened?

Is there any notifications sent upon opening, or any traversal of the view hierarchy to detect it?

Thanks

like image 288
Reuven Avatar asked Dec 06 '10 04:12

Reuven


2 Answers

They do send an alert when they open, but only to their delegate -- Check this question for a nice approach to that problem. Techzen recommends setting a boolean flag to YES when you open up the alert, and setting it back to NO when you dismiss the alert.

EDIT:

Since you don't have access at all to the code, why not try this clunky piece of code:

-(BOOL) doesAlertViewExist {
  for (UIWindow* window in [UIApplication sharedApplication].windows) {
    NSArray* subviews = window.subviews;
    if ([subviews count] > 0) {

      BOOL alert = [[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]];
      BOOL action = [[subviews objectAtIndex:0] isKindOfClass:[UIActionSheet class]];

      if (alert || action)
        return YES;
     }
  }
  return NO;
}
like image 97
Sam Ritchie Avatar answered Nov 15 '22 20:11

Sam Ritchie


- (BOOL) doesAlertViewExist {
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        for (UIView* view in window.subviews) {
            BOOL alert = [view isKindOfClass:[UIAlertView class]];
            BOOL action = [view isKindOfClass:[UIActionSheet class]];
            if (alert || action)
                return YES;
        }
    }
    return NO;
}
like image 22
Reuven Avatar answered Nov 15 '22 20:11

Reuven