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

问题描述 投票:2回答:1

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

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

但它只给我打印活动状态。我从背景状态得不到任何东西。如果我的应用程序处于后台,我怎么能注意到?我应该在哪里调用这个函数?有人有这方面的经验吗?

swift background sprite-kit state
1个回答
0
投票

由于你使用的是SpriteKit,我假设检查你所提供的state的代码是在你的didMove(to:)版本的GameScene.swift方法中调用的。如果是这种情况,那么您没有遇到问题,而是遇到了预期的结果;当场景调用didMove(to:)方法时,应用程序始终处于活动状态。

To capture each state, try implementing these three methods inside of your version of AppDelegate.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")
}
© www.soinside.com 2019 - 2024. All rights reserved.