Check if SwiftUI app is running in background

In this tutorial, I will show you how to check if the iOS app is running in the background or not in SwiftUI.

We will try to apply the detection in the whole app.

To do this we will use @EnvironmentWrapper.

We will detect the inactive, active, or background running status using @Environment(\.scenePhase).

scenePhase environment key of SwiftUi can give us updates regarding the running status of the app.

The most interesting thing about scenePhase is, that it can give updates continuously and automatically.

Using scenePhase to detect if app is running in the background or not in SwiftUI

Here is the code to do this:

import SwiftUI

@main
struct codespeedyApp: App {
    @Environment(\.scenePhase) var state
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: state, initial: true){ initial, myValue in
            switch myValue {
            case.inactive:
                print("I am inactive")
                
            case.active:
                print("I am active")
            
            case.background:
                print("I am in background")
            @unknown default: print("Default")
            }
            
        }
    }
}

I am using .onChange modifier here: line no 11.

Here cases can be of three types.

  • case.inactive
  • case.active
  • case.background

If the app is in foreground, it will show active status. If the app is minimized and running in the background, it will show the background status.

If you close the app then do not expect your app to show inactive status in the console, because the app is already closed and it can’t show anything in the console anymore.

Output of the above program:

check background running status in swiftui

If we wish, we can also do the same using if else statement as well. But I personally like switch case when it comes to handling multiple conditions.

There might be a lot of questions like why I used intial parameter in onChange modifier.

If you do not use that, you will get a warning. Will bring another tutorial on that too soon.

Note: Even if you pull down the notification, your SwiftUI app will go into inactive status. It is the iOS feature and you can’t change that. Yes, still you can keep some tasks active and it is done with complex tricks.

Detect active or running in background in SwiftUI using NotificationCenter

We can do the same using .onReceive modifier and NotigicationCenter.

import SwiftUI

@main
struct codespeedyApp: App {
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                // Checking active status
                .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { (_) in
                  print("I am active")
                }
                // Checking background status
                .onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { (_) in
                  print("I am in background")
                }
        }
            
    }
}

Here I am using .onReceive modifier to listen for the UIApplication.didBecomeActiveNotification notification, which is triggered when the app becomes active or comes to the foreground. When this notification is received, it prints “I am active” to the console.

Similarly, for background status checking, I am using UIApplication.willResignActiveNotification notification, which is triggered when the app is about to resign its active status (e.g. when it goes to the background). When this notification is received, it prints “I am in background” to the console.

Leave a Reply

Your email address will not be published. Required fields are marked *