1

I want to make a single listener which grabs UIAlertView whenever it is shown to users.

I first setup this observer.

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(windowDidBecomeVisible:)
                                                 name:UIWindowDidBecomeVisibleNotification
                                               object:nil];

and then get UIWindow object like below.

- (void)windowDidBecomeVisible:(NSNotification *)notification
{
    UIWindow *window = [notification object];
}

But from this window object, I cannot get UIAlertView. There is only one subview under this window and that subview doesn't have any subviews.

Below link shows the code to get a UIAlertView object from the window object, but this doesn't work. https://stackoverflow.com/a/2529692/1709287

Maybe iOS7 does some trick to make UIAlertView wrapped and hidden. Does anyone know how to track down UIWindow object's subviews (or members) to reach UIAlertView object which is currently shown on the screen ?

Community
  • 1
  • 1

2 Answers2

2

People commonly did this kind of thing in iOS 6.

Apple deliberately made it impossible on iOS 7. You can't do it anymore. The only option would be private APIs, and using those will get your app rejected from the store.

You have to stop using UIAlertView if you need this level of control.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
1

In iOS 7 you may not be able to get the instance of alerview through window. You will have to explicitly hold the reference some where in application.

We faced the same issue. Issue was solved with

1) Subclassing the UIAlertView say MYAlertview

2) In AppDelegate have the property of MyAlertView(You can choose any instance)

3) Override show:method

- (void)Show{
  AppDelegate *appdelegate = (AppDelegate)[[UIApplication SharedApplication] delegate];
  if([[appdelegate alertView] isVisible])
  [[appdelegate alertView] dismissWithClickedButtonIndex:0 animated:NO]; //We have requirement like dismissing you can have any operation here

  [appDelegate setAlertView:self];
}

4) Replace all the UIAlertView with MYAlertview

Rajesh
  • 10,318
  • 16
  • 44
  • 64