Using addObserver func of NotificationCenter.default, subscribing to UIApplication.willEnterForegroundNotification does not work when the backgrounding is triggered from Notification Center (swipe down from top when app is active to get Notification Center). It also seems that UIApplication.willResignActiveNotification
is called twice, based on Print() logs, when Notification Center triggers the backgrounding.
If you background the app via lock screen or manual swipe, or switch to another app, the UIApplication.willEnterForegroundNotification
does get called, so it seems to be specific to Notification Center.
UIApplication.willResignActiveNotification does fire correctly but then resuming the app never calls the appMovedToForeground
func below:
notificationCenter.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification,object: nil)
occurs on: iOS 14.4.2, iPhone 11 & iOS 14.6, SE(2)
UIApplication.willEnterForegroundNotification
is posted when the application transitions to the foreground state. Note that this is not the active state.
https://developer.apple.com/documentation/uikit/uiapplication/state/
Being foreground translates to: UIApplication.State.inactive
as well as UIApplication.State.active
. The application is foreground in both states, but when inactive
there is something (or somethings) preventing it from being active.
So when Control Center or Notification Center are presented, it would be expected that the application state moves to inactive, and UIApplication.willResignActiveNotification
is posted. When those issues are resolved, it would be expected that UIApplication.didBecomeActiveNotification
is posted, not UIApplication.willEnterForegroundNotification
.