1

I want to detect when system alertView, like in-app purchase confirmation alert appears on screen. I'm trying to add key value observer to UIApplication, but it doesn't work.

UIApplication.sharedApplication().addObserver(self, forKeyPath: "windows", options: NSKeyValueObservingOptions.allZeros, context: &myContext)
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
  • More than likely because iOS 8 doesn't use UIAlertView anymore, so alerts don't have a separate window anymore either – Scott Berrevoets Oct 29 '14 at 16:49
  • 2
    may I ask whether there is any specific reason why you need to know about the system alerts? it is quite irregular monitoring the system popups like this. – holex Oct 29 '14 at 17:03
  • I want to start animating spinner, and when alert popup stop animating. – ChikabuZ Oct 29 '14 at 17:52
  • Take a look at this: maybe it can help you: [http://stackoverflow.com/questions/6607170/can-i-get-message-when-i-show-uialertview][1] [1]: http://stackoverflow.com/questions/6607170/can-i-get-message-when-i-show-uialertview – Duyen-Hoa Oct 30 '14 at 13:40

2 Answers2

4

It's not that I wanted, but it works for me.

NotificationCenter.addObserver(self, selector: "applicationWillResignActive", name: UIApplicationWillResignActiveNotification, object: nil)
NotificationCenter.addObserver(self, selector: "applicationDidBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)

func applicationDidBecomeActive()
{
    if Library.isPurchasing
    {
        Application.networkActivityIndicatorVisible = true
    }
}

func applicationWillResignActive()
{
    if Library.isPurchasing
    {
        Application.networkActivityIndicatorVisible = false
    }
}
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
  • in swift 5 : NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil) – timyau Nov 11 '19 at 05:50
0

Look at UIAlertViewDelegate documentation. There are methods like willPresentAlertView:, didPresentAlertView:, alertView:willDismissWithButtonIndex:, and alertView:didDismissWithButtonIndex:.

AdamPro13
  • 7,232
  • 2
  • 30
  • 28