Skip to content

Instantly share code, notes, and snippets.

@VAnsimov
Last active November 6, 2020 11:29
Show Gist options
  • Save VAnsimov/e28cd68df1cd761147c5113feaf64c4e to your computer and use it in GitHub Desktop.
Save VAnsimov/e28cd68df1cd761147c5113feaf64c4e to your computer and use it in GitHub Desktop.
Application life cycle
@main
struct YourApp: App {
var body: some Scene {
let publisherBackground = NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)
let publisherForeground = NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
return WindowGroup {
RootView()
.onReceive(publisherBackground) { _ in
print("Moving to the background!")
}
.onReceive(publisherForeground) { _ in
print("Moving back to the foreground!")
}
}
}
}
@main
struct YourApp: App {
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
RootView().onChange(of: scenePhase) {
switch $0 {
case .background:
print("Moving to the background!")
case .active:
print("Moving back to the foreground!")
case .inactive:
print("App inactive!")
@unknown default:
break
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment