在 swift 中检测应用程序是在后台还是在前台

Detect if the application in background or foreground in swift

有什么方法可以知道我的应用程序处于后台模式还是前台模式。谢谢

[UIApplication sharedApplication].applicationState 将 return 应用程序的当前状态,例如:

  • UIApplicationStateActive
  • UIApplicationStateInactive
  • UIApplicationStateBackground

或者如果您想通过通知访问,请参阅 UIApplicationDidBecomeActiveNotification

Swift 3+

let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
    // background
} else if state == .active {
    // foreground
}

switch UIApplication.shared.applicationState {
    case .background, .inactive:
        // background
    case .active:
        // foreground
    default:
        break
}

Objective C

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive) {
    // background
} else if (state == UIApplicationStateActive) {
    // foreground
}

您可以在应用程序进入后台或进入前台时添加一个布尔值。 您可以通过使用 App delegate 获得此信息。

根据 Apple 文档,也许您还可以使用应用程序的主窗口 属性 或应用程序的活动状态 属性。

NSApplication documentation

Discussion The value in this property is nil when the app’s storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.

Swift 3

  let state: UIApplicationState = UIApplication.shared.applicationState

            if state == .background {

                // background
            }
            else if state == .active {

                // foreground
            }

Swift 4

let state = UIApplication.shared.applicationState
        if state == .background {
            print("App in Background")
        }else if state == .active {
            print("App in Foreground or Active")
        }

如果有人想要 swift 3.0

switch application.applicationState {
    case .active:
        //app is currently active, can update badges count here
        break
    case .inactive:
        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
        break
    case .background:
        //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
        break
    default:
        break
    }

对于swift 4

switch UIApplication.shared.applicationState {
case .active:
    //app is currently active, can update badges count here
    break
case .inactive:
    //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
    break
case .background:
    //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    break
default:
    break
}

UIViewControllerviewDidload 中使用这些观察者:

let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
nc.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

和方法:

@objc func appMovedToBackground() {    
}

@objc func appMovedToForeground() {
}