usinuniverse
usinuniverse

Reputation: 760

How to call viewController's method in appdelegate

I want viewController's method for update data.

So I want use viewController's update method when applicationDidBecomeActive(_ application: UIApplication).

how it possible?

Upvotes: 2

Views: 496

Answers (2)

Krunal
Krunal

Reputation: 79776

This may help you (tested in Swift 4)

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(applicationDidBecomeActive),
                                               name: Notification.Name.UIApplicationDidBecomeActive,
                                               object: nil)
    }

}

@objc func applicationDidBecomeActive() {
    print("UIApplicationDidBecomeActive")
}

Note: Don't forget to remove observer when your view controller is no longer in use/memory (as this is an application level observer and will be called every time your application becomes active, whether your view controller is active or not.

Here is code to remove observer:

NotificationCenter.default.removeObserver(self,
                                          name: Notification.Name.UIApplicationDidBecomeActive,
                                          object: nil)

Upvotes: 1

Alexey Kudlay
Alexey Kudlay

Reputation: 525

Use UIApplicationDidBecomeActiveNotification. See this answer.

Upvotes: 2

Related Questions