Search code examples
iosswiftuiapplicationdelegate

Application delegate event(ex.: applicationWillTerminate) to my custom class


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


Solution

  • 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