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

问题描述 投票:0回答:7

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

ios swift lifecycle
7个回答
158
投票

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

  • UIApplicationStateActive
  • UIApplicationStateInactive
  • UI应用程序状态背景

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

斯威夫特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
}

目标C

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

18
投票

斯威夫特3

  let state: UIApplicationState = UIApplication.shared.applicationState

            if state == .background {

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

                // foreground
            }

14
投票

在您的

viewDidload
UIViewController
中使用这些观察者:

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() {
}

8
投票

斯威夫特4

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

3
投票

如果有人想要 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
}

1
投票

当应用程序进入后台或进入前台时,您可以添加一个布尔值。 您可以通过使用应用程序委托来获取此信息。

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

NS应用文档

讨论 当应用程序的故事板或 nib 文件尚未完成加载时,此属性中的值为 nil。当应用程序处于非活动状态或隐藏时,它也可能为零。


0
投票

我找到了解决方案并在另一个问题中回答了它。我提供了一个链接,您可以在其中获得答案。

https://stackoverflow.com/a/78075214/8946720

© www.soinside.com 2019 - 2024. All rights reserved.