How does the scene phase change in the SwiftUI app?

ByungwookAn
3 min readNov 7, 2023

--

Intro

When you make a new SwiftUI project, you can see the basic code that is automatically written. Today, I’ll simply analyze this basic code in the new SwiftUI project first, and introduce the Scene and Scene phases.

App launch and initialization

When you tap the app’s icon, the app is launched. After that, The system loads the app’s main entry point which is defined in the @main.

The @mainentry point usually initializes the app's main App struct, which conforms to the App protocol. and this is the starting point of the app.

In the body, the Initial view of this app is defined. In this code, ContentView() is an initial view of this app.

What does Scene mean?

In this code, you can see the Scene protocol. Scene is a part of an app’s user interface with a life cycle managed by the system. The Scene represents a specific instance of your app’s UI, which can be a window or a view hierarchy.

You can see more explanation in this Apple document.

What kinds of phases the Scene may go through?

The scene has different phases. Active, Inactive, and Background. These phases reflect the state of the scene and allow your app to respond accordingly. Before explaining these phases, I’ll show you a sample code.

import SwiftUI

struct ContentView: View {
@Environment(\.scenePhase) var scenePhase

var body: some View {
Text("Hello, world!")
.padding()
.onChange(of: scenePhase) {
if scenePhase == .active {
print("Active")
} else if scenePhase == .inactive {
print("Inactive")
} else if scenePhase == .background {
print("Background")
}
}
}
}

If you run this code, you can easily understand scene phases. At first, When you launch this app, “Active” is printed. and when you swipe up your test app, “Inactive” is printed. When you swipe up your app totally, the “Background” is printed.

When you run the app again in background mode, You can find the app’s phases changed to Inactive and Active. The important part is that before the active phase, the Inactive phase is running first.

In conclusion, the Active phase is visible and currently in use by the user. the InActive phase is running but user interaction with your app is temporarily suspended. When the user receives a notification, It occurs. Finally, the Background phase is no longer visible to the user. The scene is still running, but it’s not actively executing code related to the UI.

Why do we have to understand scene phases?

Understanding scene phases is essential because it allows respond to various lifecycle events and manage their app’s behavior effectively. Here are some reasons why we need to understand scene phases:

  1. Lifecycle Management: Scene phases represent different points in the lifecycle of an app or scene. By responding to these phases, developers can initialize and deinitialize resources, save and restore the app state.
  2. Resource Management: Scene phases allow you to allocate and release resources efficiently. For example, when your app enters the inactive phase, you can release unnecessary resources to free up memory. When it becomes active again, you can reinitialize those resources.
  3. Handling Errors: By understanding scene phases, you can handle potential errors or issues that may occur during various phases, such as data corruption, network problems, or background tasks that fail to complete.

Source

--

--