我想用Swift来了解我的macOS应用是否在前台运行。
我发现了与iOS应用相关的不同方式(例如:Is there any way to check if iOS app is in background?),但与现代macOS无关。
如果macOS也具有与此类似的方式,那就太好了
let state = NSApplication.applicationState
if state == .background { // doesn't work
// do something here
}
答案 0 :(得分:0)
我发现是这样的:
let application = NSApplication.shared
let state = application.isActive
if state {
print("app in foreground")
}