Last active
November 6, 2020 11:29
-
-
Save VAnsimov/e28cd68df1cd761147c5113feaf64c4e to your computer and use it in GitHub Desktop.
Application life cycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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!") | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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