【问题标题】:How to know if my macOS app is in background or in foreground?如何知道我的 macOS 应用程序是在后台还是前台?
【发布时间】:2019-05-06 21:31:40
【问题描述】:

我想通过 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
   }

【问题讨论】:

标签: swift macos nsapplication nsworkspace


【解决方案1】:

我是这样找到的:

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

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

【讨论】:

    猜你喜欢
    • 2015-01-08
    • 2011-05-02
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多