sceneDidEnterBackground not called from App Switcher

When I go from my app to the App Switcher by double-clicking the home button, the app is shown in the App Switcher view, still running. However, if I then kill the app by sliding it upward, it is killed without sceneDidEnterBackground ever having been called. This means the saved state ends up being whatever it was when the app was launched.

In an older app, which uses applicationDidEnterBackground, this problem does not exist; the method is always called, regardless of how I exit it, including when the app is killed by sliding it upward in the App Switcher.

Isn't sceneDidEnterBackground supposed to be called at the same moments as applicationDidEnterBackground in older apps? As far as I can see, it isn't, and I don't see how to get my app to write its state reliably. Saving state in sceneWillResignActive is not a good alternative, because in the App Switcher, the app is still running after that method is called, so whichever changes happen to its state after sceneWillResignActive is called would still be lost.

IMHO, it should be equivalent to AppDelegate:

https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle

Note from documentation:

When you implement this method and enable scenes, UIKit calls this method but does not call the applicationDidEnterBackground(_:) method on UIApplicationDelegate.

Did you check scene manifest in info.plist ?

This is the state that isn't handled correctly. This is after double-pressing the home button. You can't see it in the screen shot, but in this state, the app is still running, that is, the "life" algorithm is still running and continuously updating the screen. The sceneWillResignActive method was called, but sceneDidEnterBackground was not. At this point, that is all still OK, since the app is still active.

But if I swipe the app upward at this point, sceneDidEnterBackground is still not getting called, and I don't understand why. In an older app of mine, where the lifecycle is managed using the AppDelegate methods, applicationDidEnterBackground is called when swiping up. And also note: if I single-press the home button while in the app, it is placed in the background, and the sceneWillResignActive and sceneDidEnterBackground are both called, as expected.

(Sorry, there's supposed to be a screen shot here, but that doesn't seem to work...)

[object Object]

I didn't find a solution with SceneDelegate, so I removed it and implemented applicationDidEnterBackground instead. Not very elegant, and if I ever write an app where I really need UIScene, I will have to revisit this issue. But for my current app, using the application delegate methods gets the job done, it's saving it state the way I want it regardless of how the app is exited.

sceneDidEnterBackground not called from App Switcher
 
 
Q