is there any way to know the state of my application if it is in background mode or in foreground . Thanks
To detect if an iOS application is in background or foreground we can simply use the UIApplication just like we can use it to detect many other things like battery state, status etc. The shared. application state is an enum of type State, which consists of the following as per apple documentation.
This is done using three steps: Adding a new property to watch an environment value called scenePhase . Using onChange() to watch for the scene phase changing. Responding to the new scene phase somehow.
[UIApplication sharedApplication].applicationState
will return current state of applications such as:
or if you want to access via notification see 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 }
Swift 3
let state: UIApplicationState = UIApplication.shared.applicationState if state == .background { // background } else if state == .active { // foreground }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With