swift - 如何检测 swift 应用程序何时从后台进入前台?
问题描述
如果应用程序不是从后台运行,我想在我的应用程序中运行一个函数。我怎样才能做到这一点?
我不能使用 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.
}
推荐阅读
- javascript - 带有 for 循环的数组迭代 - 使用条件语句和返回
- node.js - 无法使用终端执行我的 node.js 代码
- nexmo - 如何安装用于创建语音应用程序的库
- c++ - 传递显式空参数包
- android - Android Studio 的“Profile or Debug APK”显示了错误的库大小,并声称它们没有被剥离
- r - 创建新的列数据框函数和 lapply:提供太多变量
- python - Django - TIME_ZONE 和 timezone.now()
- javascript - 如何在元素悬停时将样式应用于多个类?
- python - 无法使用 api 将数字列表保存到模型
- tomcat - 得到错误源服务器没有找到目标资源的当前表示或不愿意透露存在的表示