DEV Community

Wesley de Groot
Wesley de Groot

Posted on • Originally published at wesleydegroot.nl on

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
    }
Enter fullscreen mode Exit fullscreen mode
  • .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:

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of DataStax

Langflow: Simplify AI Agent Building

Langflow is the easiest way to build and deploy AI-powered agents. Try it out for yourself and see why.

Get started for free

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay