Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i check if any UIAlertView displaying right now? [duplicate]

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?

like image 708
Padavan Avatar asked Jan 17 '14 07:01

Padavan


2 Answers

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.

like image 153
IronManGill Avatar answered Oct 14 '22 18:10

IronManGill


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;
}
like image 31
Bhavin_m Avatar answered Oct 14 '22 19:10

Bhavin_m