Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Swift how reload viewcontroller when app come from background?

Tags:

ios

swift

I want when user call app from background to reaload one function from viewcontroller?

like image 486
chris antonopoulos Avatar asked Dec 24 '22 01:12

chris antonopoulos


1 Answers

Elaborating on nishith's answer:

Add the following code to your view controller you want to refresh

override func viewWillAppear() {
    super.viewWillAppear()
......
......
NotificationCenter.default.addObserver(self, selector:#selector(YourViewController.methodToRefresh), name: UIApplication.willEnterForegroundNotification, object: UIApplication.shared)
......
......
}

Always remember to cleanup when the view disappears in:

    override func viewWillDisappear(animated: Bool) {
    NotificationCenter.default.removeObserver(self)
}
like image 147
Abhishek Arora Avatar answered Jan 19 '23 00:01

Abhishek Arora