有什么方法可以知道我的应用程序处于后台模式还是前台模式。谢谢
最佳答案
[UIApplication sharedApplication].applicationState
将返回应用程序的当前状态,例如:
- UIApplicationStateActive
- UIApplicationStateInactive
- UIApplicationStateBackground
或者如果您想通过通知访问,请参阅 UIApplicationDidBecomeActiveNotification
swift 3+
let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
// background
} else if state == .active {
// foreground
}
switch UIApplication.shared.applicationState {
case .background, .inactive:
// background
case .active:
// foreground
default:
break
}
objective-c
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive) {
// background
} else if (state == UIApplicationStateActive) {
// foreground
}
关于ios - 快速检测应用程序是在后台还是在前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38971994/