0

Why isn't iOS calling viewWillAppear when our application is returning from the background, even when I've set UIModalPresentationStyle.FullScreen?

Le-roy Staines
  • 2,037
  • 2
  • 22
  • 40
  • You can read about this here: https://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-backgroun – πter Apr 01 '20 at 11:57

1 Answers1

1

viewWillAppear is a function that responds to a view controller's state change. Background and foreground states are different; they are done at an app level.

You can still respond app state changes by using notifications:

override func viewDidAppear(_ animated: Bool) {
    // ...
    NotificationCenter.default.addObserver(self, selector: #selector(didReceiveForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
}

@objc func didReceiveForegroundNotification() {
    // app returned from the background
}

Your view controller will listen to events until it is deallocated or removed as an observer. If you don't want to execute code when the view controller has dissappeared, you can do it on viewDidDisappear:

override func viewDidDisappear(_ animated: Bool) {
    // ..
    NotificationCenter.default.removeObserver(self)
}
folverap
  • 126
  • 3