I am trying to understand the iOS application lifecycle. I ran into this issue with an app I was working on, and to simplify the problem, started a new Xcode project and just added print statements to the lifecycle events. When I initially open the app, those lifecycle events run as expected. If I close the app and reopen, none of them run.
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
print ("Will connect.")
}
func sceneDidDisconnect(_ scene: UIScene) {
print ("Did disconnect.")
}
func sceneDidBecomeActive(_ scene: UIScene) {
print ("Did become active.")
}
func sceneWillResignActive(_ scene: UIScene) {
print ("Will resign active.")
}
func sceneWillEnterForeground(_ scene: UIScene) {
print ("Will enter foreground.")
}
func sceneDidEnterBackground(_ scene: UIScene) {
print ("Did enter background.")
}
}
Prints as expected:
Will connect.
Will enter foreground.
Did become active.
Will resign active.
Did enter background.
Did disconnect.
When the app reopens, nothing prints.
When you launch it independently, the console output isn’t visible. This is why you don’t see lifecycle print statements after closing and reopening the app outside of Xcode.
To grasp this, imagine how Xcode connects a debugger to the running app. When you start the app from Xcode, the debugger keeps an eye on everything, grabs logs, and shows important events in the console. But when you close the app, the debugger lets go. When you start it again, it starts up normally, but without the debugger, so you don’t see any logs.
This is a common thing in iOS development. Logs that show up in Xcode’s console only appear when you’re using the debugger. If you want to keep logs around for longer, you can use tools like OSLog or create your own logging system.