Last active
December 16, 2015 05:09
-
-
Save nubbel/5382154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// DetectAlertView | |
// | |
// Created by Dominique d'Argent on 4/14/13. | |
// Copyright (c) 2013 Dominique d'Argent. All rights reserved. | |
// | |
#import "ViewController.h" | |
@implementation ViewController | |
- (void)viewDidAppear:(BOOL)animated { | |
[super viewDidAppear:animated]; | |
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; | |
[self.view addSubview:spinner]; | |
[spinner startAnimating]; | |
// Show alert view in 2 seconds | |
double delayInSeconds = 2.0; | |
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); | |
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ | |
[[[UIAlertView alloc] initWithTitle:@"Alert" | |
message:@"Message" | |
delegate:nil | |
cancelButtonTitle:@"Cancel" | |
otherButtonTitles:@"OK", nil] show]; | |
}); | |
[[NSNotificationCenter defaultCenter] addObserverForName:UIWindowDidBecomeKeyNotification | |
object:nil | |
queue:[NSOperationQueue mainQueue] | |
usingBlock:^(NSNotification *note) { | |
UIWindow *window = [note object]; | |
for (UIView *view in window.subviews) { | |
if ([view isKindOfClass:[UIAlertView class]]) { | |
NSLog(@"ALERT!!!"); | |
[spinner stopAnimating]; | |
} | |
} | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Although this looks very promising, it does not work in my scenario (both the dispatch_async and UINotification approach). This is probably because of how the alert is displayed in my case (it's a system alert). There is no UIAlertView in the view hirarchy of any window. This works for me: