0

I am creating one application where I have created my custom static class where I want to catch application level events like (applicationDidBecomeActive, applicationWillResignActive and applicationWillTerminate).

Say Ex: I want to set one variable in my custom class called : IS_ACTIVE, which is default false

class CustomClass {
    static var IS_ACTIVE: Bool = false;
}

I want to set IS_ACTIVE to true when applicationDidBecomeActive event occurs,

right now I am setting up IS_ACTIVE via "AppDelegate.swift" file where applicationDidBecomeActive occurs,

but I want is applicationDidBecomeActive should occur in my custom class.

please help me on this. Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Hi @rmaddy, It's not duplicate or same as you mentioned. because I do not want to integrate with view controller. I want to catch that event in simple custom class( which is not any controller. ) – Ankur Patel May 09 '18 at 04:21
  • A view controller is just an example of another class. It's still the same question and solution. – rmaddy May 09 '18 at 04:25
  • Yes got it but it doesn't work out for me. any suggestion? – Ankur Patel May 09 '18 at 04:28
  • Not without setting your attempted code. I suggest posting a new question with your attempt to listen and respond to the notification. – rmaddy May 09 '18 at 04:29

1 Answers1

1

As my knowledge, you should add observer to listen app event look like this

NotificationCenter.default.addObserver(self, selector: #selector(self.appBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

And remember remove observer when your class deinit

baohung02
  • 91
  • 1
  • 7