Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find UIAlertView without having reference to it iOS 7

I was using a code snippet in my project answered here: UIAlertView without having reference to it

Here's the code:

+ (UIAlertView *) getUIAlertViewIfShown {
    if ([[[UIApplication sharedApplication] windows] count] == 1) {
        return nil;
    }

    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    if ([window.subviews count] > 0) {
        UIView *view = [window.subviews objectAtIndex:0];
        if ([view isKindOfClass:[UIAlertView class]]) {
            return (UIAlertView *) view;
        }
    }
    return nil;
}

Unfortunately its not working in iOS 7 and I'm unable to dismiss an alert view. While debugging I found that in the loop its showing that view is of class UITransitionView. Pretty confusing because I couldn't find any quick documentation for this view class.

Any ideas how can I fix this problem?

like image 461
Abdullah Umer Avatar asked Sep 09 '13 16:09

Abdullah Umer


2 Answers

In iOS7, the UIAlertView window does not appear in -[UIApplication windows]. In fact, the UIAlertView itself is never added to any window, -[UIAlertView window] is always nil. Instead, the alert view manages a variety of undocumented views placed in -[UIApplication keyWindow] with no reference back to the alert view.

Your only real option in iOS7 is to actually keep track of your alert views.

like image 145
Brian Nickel Avatar answered Sep 30 '22 11:09

Brian Nickel


iOS 7 solution

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

I am not sure if it is approvable by AppStore, but works

UPD single line code:

UIAlertView *topMostAlert = [NSClassFromString(@"_UIAlertManager") performSelector:@selector(topMostAlert)];
like image 37
storoj Avatar answered Sep 30 '22 11:09

storoj