
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to detect if an iOS application is in background or foreground?
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.
Let’s see how we can do this in our application. We’ll make use of shared resources of our Application which are stored in UIApplication.shared. We can use it like shown below −
print(UIApplication.shared.applicationState)
The shared.application state is an enum of type State, which consists of the following as per apple documentation.
public enum State : Int { case active case inactive case background }
The case active means that the application is in the foreground and is receiving events like touch events or any other event that can keep the application active.
Case Inactive means that the application is running in the foreground but is not receiving any events.
Case background means that the application is running in the background.
We can use it according to our needs like shown above. We can also perform certain operations depending on conditions.
let state = UIApplication.shared.applicationState if state == .active { print("I'm active") } else if state == .inactive { print("I'm inactive") } else if state == .background { print("I'm in background") }
When we run this in viewDidLoad of our application we get the following result:
- Related Articles
- How to check if an iOS program is in foreground or in background?
- How to detect when an Android app goes to the background and come back to the foreground?
- How to check if an Android application is running in the background?
- How to check if android application is running background?
- How to check if an Android application is running in the background using Kotlin?
- How to change JLabel background and foreground color in Java?
- How to detect a long press in iOS?
- Background and foreground thread in C#
- How to detect user pressing HOME key in iOS?
- How to change the background and foreground colors of tab in Java?
- How to detect application heap size in android?
- How to detect which iOS version is running on the device?
- Difference Between Foreground and Background Selection
- How to set background for Navigation Bar in iOS?
- How to detect user inactivity for 5 seconds in iOS?
