Le-roy Staines
Le-roy Staines

Reputation: 2057

iOS viewWillAppear not being called when returning from background, even with UIModalPresentationStyle.FullScreen, in iOS 13+

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

Upvotes: 0

Views: 494

Answers (1)

folverap
folverap

Reputation: 126

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)
}

Upvotes: 1

Related Questions