6

Can I in any part of my iOS application check if any UIAlertView displaying right now? I found this code

[NSClassFromString(@"_UIAlertManager") performSelector:@selector(topMostAlert)]

but it is private API and my app could be rejected by Apple. Are there any legal ways to do this?

Padavan
  • 1,056
  • 1
  • 11
  • 32
  • Check this question: [link](http://stackoverflow.com/q/10727957/2298998) – iOS Dev Jan 17 '14 at 07:15
  • It is a private API but somewhere I read that using performselector is ok with Apple's static code analysis. But you need to be double sure. – Kumar Aditya Jan 17 '14 at 07:35

2 Answers2

7

For devices less than iOS 7 :-

When you make the body of the UIAlertView , the [alertView Show] adds a subview on the main window. So to detect the UIAlertView you simply have to check for the subviews of the current UIView as UIAlertView inherits from UIView.

for (UIWindow* window in [UIApplication sharedApplication].windows){
    for (UIView *subView in [window subviews]){
        if ([subView isKindOfClass:[UIAlertView class]]) {
            NSLog(@"AlertView is Present");
        }else {
            NSLog(@"AlertView Not Available");
        }
    }
}

EDIT:- For devices using iOS 7

Class UIAlertManager = objc_getClass("_UIAlertManager");
UIAlertView *Alert = [UIAlertManager performSelector:@selector(Alert)];

and

UIAlertView *Alert = [NSClassFromString(@"_UIAlertManager") performSelector:@selector(Alert)];

NOTE :- If you cannot use third party API's then your only real option in iOS7 is to actually keep track of your alert views.

IronManGill
  • 7,222
  • 2
  • 31
  • 52
1

you can check like this.

-(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;
}
Bhavin_m
  • 2,746
  • 3
  • 30
  • 49