I want when user call app from background to reaload one function from viewcontroller?
Asked
Active
Viewed 5,351 times
7
-
use some flag value in NSUserdefault when in background mode and check if value set true then called and after call again set the flag to false hope make sense!! – Sanjeet Verma Sep 27 '16 at 08:50
-
if call from viewcontroller from background the function who calls again is viewdidload() ? – chris antonopoulos Sep 27 '16 at 09:09
-
If anyone is looking for solution for swift 3 go here: https://stackoverflow.com/a/34529572/6595705 – stream28 Jun 23 '17 at 11:03
2 Answers
9
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)
}

Abhishek Arora
- 395
- 2
- 12
4
You can register your controller for these notifications and reload your controller accordingly.
UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification

nishith Singh
- 2,968
- 1
- 15
- 25
-
At AppDelage i had this functions and i had enable backgroud modes. – chris antonopoulos Sep 27 '16 at 09:15
-
If you have implemented your functionality in those methods then, what problem are you facing exactly? – nishith Singh Sep 27 '16 at 09:18
-
sry this functions is in AppDelegate not at viewcontroller. This function observes the state of application inside the controller? – chris antonopoulos Sep 27 '16 at 09:20
-
So you can register for these notifications in the viewDidLoad of your UIViewController and then you can handle these events in your UIViewController also – nishith Singh Sep 27 '16 at 09:22
-