iOS App States
When you're developing an iOS app, it's important to understand the different states that your app can be in, to handle them correctly and provide a smooth user experience. In this post, we'll take a look at the different app states in iOS and how you can work with them.
Not Running
The app is not running. This is the initial state of the app when it's not launched or has been terminated by the system.
Inactive
The app is running in the foreground but is not receiving events. This can happen when the app is transitioning to a different state, such as when a phone call or SMS message is received, pr when Control Center or Notification Center is displayed.
Active
The app is running in the foreground and is receiving events. This is the normal state for the app when it's active and the user is interacting with it.
Background
The app is running in the background (not visible for the user) and performing background tasks. This can happen when the user switches to another app or when the app is suspended by the system.
Suspended
The app is in the background but is not executing tasks. This is the state when the app is in the background but is not performing any tasks. The system can terminate the app in this state to free up resources.
The OS can move the app to this state after it has been in the background for a while and is not performing any tasks.
Responding to App State Changes
You can respond to app state changes by adding the following modifiers to your SwiftUI views:
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.willResignActiveNotification
)) { _ in
// app is going to the background, so do something
}
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.willEnterForegroundNotification
)) { _ in
// app is coming to the foreground, so do something else
}
.onAppear
- Called when the view appears on the screen..onDisappear
- Called when the view disappears from the screen..onReceive
- Called when the view receives a notification..onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification))
- Called when the app is about to enter the foreground..onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification))
- Called when the app enters the background..onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification))
- Called when the app is about to become inactive..onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification))
- Called when the app becomes active..onReceive(NotificationCenter.default.publisher(for: UIApplication.willTerminateNotification))
- Called when the app is about to be terminated..onReceive(NotificationCenter.default.publisher(for: UIApplication.didFinishLaunchingNotification))
- Called when the app has finished launching..onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification))
- Called when the app is about to enter the foreground..onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification))
- Called when the app enters the background..onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification))
- Called when the app is about to become inactive..onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification))
- Called when the app becomes active.
Resources:
Read more
- self, Self, and Self.self in Swift • 3 minutes reading time.
- @Environment variables • 4 minutes reading time.
- SwiftLeeds 2024, Day 1 • 11 minutes reading time.
Share
Share Mastodon Twitter LinkedIn Facebook