Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Created September 26, 2024 14:57
Show Gist options
  • Save lucianoschillagi/b60ba2a50f8891706dfc1d64f0de04a6 to your computer and use it in GitHub Desktop.
Save lucianoschillagi/b60ba2a50f8891706dfc1d64f0de04a6 to your computer and use it in GitHub Desktop.
SwiftUI Scene Phases
import SwiftUI
@main
struct MyApp: App {
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
.onChange(of: scenePhase) { phase in
switch phase {
case .active:
print("Now the scene is Active")
// Active: The scene is in the foreground and interactive.
case .inactive:
print("Now the scene is Inactive")
// Inactive: The scene is in the foreground and interactive.
case .background:
print("Now the scene is in Background")
// Background: The scene isn’t currently visible in the UI.
// Perform cleanup when all scenes within
// MyApp go to the background.
@unknown default:
break
}
}
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "moon")
.imageScale(.large)
.foregroundColor(.red)
.padding(.bottom)
Text("Hello, Moon!")
.font(.title)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment