我想知道斯威夫特,我的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
   }

最佳答案

我是这样发现的:

    let application = NSApplication.shared
    let state = application.isActive

    if state {
        print("app in foreground")
    }

10-05 20:17
查看更多