nehaSingh
nehaSingh

Reputation: 71

Call Appdelegate method from a custom framework

I'm currently developing a framework through which i want to check if the app has been launched and if it is in foreground or not.

The thing is that UIApplication cannot be imported in a framework. Is there a way to catch that event without having to do it in the app ?

Upvotes: 1

Views: 240

Answers (1)

ObjectAlchemist
ObjectAlchemist

Reputation: 1127

Instead of accessing the AppDelegate, which is a bad coding style in my opinion, you may listen for the events instead:

func startListen() {
    NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground(_:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}

func stopListen() {
    NotificationCenter.default.removeObserver(self)
}

func applicationWillEnterForeground(_ notification: NSNotification) {
    print("App moved to foreground!")
}

There are more notification types like UIApplicationDidEnterBackground that may notify you about the whole app lifecycle.

Upvotes: 1

Related Questions