0

I have an app where I clean up some UI when the app moves to the background. The notification of the app moving to the background occurs if, for example, on the iPad, the user swipes up to invoke the task switcher. However, the app continues to operate while the task switcher is open. How can I distinguish this from the app moving to background and not visible in the task switcher? Interestingly, if I launch the app from Xcode, the app does not continue in the task switcher. However, if it's not connected to Xcode, it continues.

For debugging purposes, I've linked the app delegate to the main view controller so I can send messages for display when the app delegate receives calls. I did this because the behavior doesn't happen when connected to Xcode, so a print will not work. I haven't found a method or notification yet that indicates this state.

Victor Engel
  • 2,037
  • 2
  • 25
  • 46
  • Doesn't `SceneDelegate`'s `sceneWillResignActive(_:)` function run when the task switcher is invoked? https://developer.apple.com/documentation/uikit/uiscenedelegate/3197919-scenewillresignactive – West1 May 16 '21 at 15:47
  • The app has no SceneDelegate. However, I can observe scene delegate notifications. I will post my results in an answer. – Victor Engel May 16 '21 at 16:29
  • I needed to distinguish between `UIScene.didEnterBackgroundNotification` where I needed to suspend everything and `UIScene.willDeactivateNotification` where I could keep the app going (for example, while visible in the task switcher). – Victor Engel May 16 '21 at 16:47

1 Answers1

2

Here is the flow of the app, noting specific function calls or notification calls.

App is launched.

viewDidAppear in viewController is called

UIScene.didActivateNotification notification is received.

applicationDidBecomeActive: is called in AppDelegate.

Task switcher is invoked by panning up slightly from bottom if iPad.

UIScene.willDeactivateNotification notification is received.

applicationWillResignActive: is called in AppDelegate.

Release task switcher, thus keeping app on screen, but icons now appear over app at bottom (stop panning after a brief pan)

UIScene.didActivateNotification notification is received.

applicationDidBecomeActive: is called in AppDelegate.

Task switcher again - this time continue panning until multiple apps appear in task switcher

UIScene.willDeactivateNotification notification is received.

applicationWillResignActive: is called in AppDelegate.

Open Safari (this time we're switching to a different app instead of returning to our app Pan up to open task switcher (at this point the app is no longer running)

UIScene.didEnterBackgroundNotification was called at some point when the app was not visible.

Open App

applicationDidBecomeActive: is called in AppDelegate.

Victor Engel
  • 2,037
  • 2
  • 25
  • 46