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?
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.
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)];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With