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?
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.
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;
}
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