 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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:

