gpt4 book ai didi

swift - 如何检测 swift 应用程序何时从后台变为前台?

转载 作者:行者123 更新时间:2023-12-05 01:34:53 27 4
gpt4 key购买 nike

如果应用程序不在后台运行,我想在我的应用程序中运行一个函数。我该怎么做?

我不能使用 applicationDidBecomeActive 和 applicationDidEnterBackground,因为我需要检测它来自后台时的情况,而不是它被带到后台或打开时的状态。还有,第一次打开应用时,从后台来的时候是不会工作的。

最佳答案

检查当前状态:

使用UIApplication.shared.applicationState

switch UIApplication.shared.applicationState {
case .active: // The app is running in the foreground and currently receiving events.
case .inactive: // The app is running in the foreground but is not receiving events. This might happen as a result of an interruption or because the app is transitioning to or from the background.
case .background: // The app is running in the background.
}

关于swift - 如何检测 swift 应用程序何时从后台变为前台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63504007/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com