我进行了一些讨论,例如this one,并尝试使用以下建议代码:
let state = UIApplication.shared.applicationState
if state == .background {
print("App in Background")
}else if state == .active {
print("App in Foreground or Active")
}
但它只给我打印活动状态。我从背景状态得不到任何东西。如果我的应用程序处于后台,我怎么能注意到?我应该在哪里调用这个函数?有人有这方面的经验吗?
由于你使用的是SpriteKit,我假设检查你所提供的state
的代码是在你的didMove(to:)
版本的GameScene.swift
方法中调用的。如果是这种情况,那么您没有遇到问题,而是遇到了预期的结果;当场景调用didMove(to:)
方法时,应用程序始终处于活动状态。
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")
}