检测应用程序进入后台并返回前台
Detecting application becoming going to the background and coming back to the foreground
我有一个 macOS 应用程序,它需要在应用程序失去焦点(进入后台)时进行一些清理,并在它再次获得焦点(应用程序现在回到前台)时重新加载一些东西。
我已经在视图和主应用程序场景中尝试过这段代码:
struct ContentView: View {
@Environment(\.scenePhase) var scenePhase
var body: some View {
Text("Hello, world!")
.padding()
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
print("Active")
} else if newPhase == .inactive {
print("Inactive")
} else if newPhase == .background {
print("Background")
}
}
}
}
但我只收到 active
状态。没有其他的。
我也试过:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { (_) in
print("UIApplication: active")
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { (_) in
print("UIApplication: background")
}
但我无法让它工作。
我如何检测应用程序何时进入后台,然后用户将其带回前台进行处理?
编辑:
我创建了一个 appdelegate 并且它触发了,但只是有时
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidBecomeActive(_ aNotification: Notification) {
print(">> app coming back, reloading data...")
}
}
这对我一直有效:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { phase in
if phase == .inactive || phase == .background {
// save
}
}
}
}
我写的原始代码有效:
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification))
将此添加到视图中:
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) {
print("coming back!")
}
}
但我的项目是多平台的,它产生了一些问题。我把这个项目变成了一个仅限 macOS 的项目并且它成功了。
我有一个 macOS 应用程序,它需要在应用程序失去焦点(进入后台)时进行一些清理,并在它再次获得焦点(应用程序现在回到前台)时重新加载一些东西。
我已经在视图和主应用程序场景中尝试过这段代码:
struct ContentView: View {
@Environment(\.scenePhase) var scenePhase
var body: some View {
Text("Hello, world!")
.padding()
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
print("Active")
} else if newPhase == .inactive {
print("Inactive")
} else if newPhase == .background {
print("Background")
}
}
}
}
但我只收到 active
状态。没有其他的。
我也试过:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { (_) in
print("UIApplication: active")
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { (_) in
print("UIApplication: background")
}
但我无法让它工作。
我如何检测应用程序何时进入后台,然后用户将其带回前台进行处理?
编辑:
我创建了一个 appdelegate 并且它触发了,但只是有时
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidBecomeActive(_ aNotification: Notification) {
print(">> app coming back, reloading data...")
}
}
这对我一直有效:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { phase in
if phase == .inactive || phase == .background {
// save
}
}
}
}
我写的原始代码有效:
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification))
将此添加到视图中:
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) {
print("coming back!")
}
}
但我的项目是多平台的,它产生了一些问题。我把这个项目变成了一个仅限 macOS 的项目并且它成功了。