1

I want to remove all UIAlertview in my application when i enter to background mode. I am using the following code to do that.

for (UIWindow* w in [UIApplication sharedApplication].windows)
    for (NSObject *o in w.subviews)
    {
        if ([o isKindOfClass:[UIAlertView class]])
        {
            [(UIAlertView *)o dismissWithClickedButtonIndex:[(UIAlertView *)o cancelButtonIndex] animated:YES];
        }
    }

It is working properly in iOS6. When i run the same application in iOS7 device it is not working. Have any ideas?

Gowtham R
  • 786
  • 2
  • 9
  • 17

4 Answers4

2

There is _UIAlertManager private class which has a method topMostAlert which returns top most alert. This works in iOS 7.0 also.

UIAlertView *topAlert = [NSClassFromString(@"_UIAlertManager") 
performSelector:@selector(topMostAlert)]
reamd
  • 41
  • 5
1

Try This one

UIWindow *window = [UIApplication sharedApplication].keyWindow;
for (UIView *view in w.subviews) {
    if ([view isKindOfClass:[UIAlertView class]]) {
        [(UIAlertView *)view dismissWithClickedButtonIndex:[(UIAlertView *)view cancelButtonIndex] animated:YES];
    }
}
Hemant Chittora
  • 3,152
  • 3
  • 19
  • 25
0

Please use this hope it will help you

    for (UIWindow* w in [UIApplication sharedApplication].windows) {
      for (NSObject *o in w.subviews) {
         for (NSObject *secnonO in o.subviews) {
          if ([secnonO isKindOfClass:[UIAlertView class]]) {
              [(UIAlertView *)o dismissWithClickedButtonIndex:[(UIAlertView *)o cancelButtonIndex] animated:YES];
           }
         }
       }
   }
Hemant Chittora
  • 3,152
  • 3
  • 19
  • 25
Abhishek Gupta
  • 601
  • 5
  • 16
-1

Use this recursive method

- (void)removeAlert:(NSArray *)subviews {
    for (UIView * subview in subviews){
        if ([subview isKindOfClass:[UIAlertView class]]){
            [(UIAlertView *)subview dismissWithClickedButtonIndex:[(UIAlertView *)subview cancelButtonIndex] animated:NO];
        } else {
            [self removeAlert:subview.subviews];
        }
    }
}

Call it from applicationDidEnterBackground like this

[self removeAlert:application.windows];

Hope this helps.

Surya Subenthiran
  • 2,217
  • 1
  • 15
  • 22