Swift spritekit无法从后台状态获取打印

Swift spritekit无法从后台状态获取打印,swift,background,sprite-kit,state,Swift,Background,Sprite Kit,State,我进行了一些讨论,例如一次讨论,并尝试使用以下建议的代码: let state = UIApplication.shared.applicationState if state == .background { print("App in Background") }else if state == .active { print("App in Foreground or Active") } 但它只在活动状态下给我打印。我从背景状

我进行了一些讨论,例如一次讨论,并尝试使用以下建议的代码:

  let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
    }else if state == .active {
        print("App in Foreground or Active")
    }
但它只在活动状态下给我打印。我从背景状态中什么也得不到。
如果我的应用程序在后台,我如何获得通知?我应该在哪里调用这个函数?有人遇到过这个问题吗?

由于您使用的是SpriteKit,我假设您提供的检查
状态的代码正在
游戏场景.swift的
didMove(to:)
方法的某处调用。如果是这样,那么你没有遇到问题,而是得到了期望的结果;当场景调用
didMove(to:)
方法时,应用程序始终处于活动状态

要捕获每个状态,请尝试在您的
AppDelegate.swift版本中实现这三种方法:
Swift 4

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    print("app finished launching")
    return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
    print("app in background")
}

func applicationWillEnterForeground(_ application: UIApplication) {
    print("app in foreground")
}