Search code examples
swiftuiios13appdelegatensnotificationcenteruiscenedelegate

Determine background status with SwiftUI


When using SwiftUI with iOS 13+, the traditional means of determining background state no longer work. For example:

  • AppDelegate methods applicationDidEnterBackground(_ application: UIApplication) and applicationDidBecomeActive(_ application: UIApplication) do not get called.

  • Notifications didEnterBackgroundNotification, willEnterForegroundNotification, didBecomeActiveNotification and willResignActiveNotification do not get sent.

As an alternative, there are UIWindowSceneDelegate callbacks: sceneDidBecomeActive(_ scene: UIScene), sceneWillResignActive(_ scene: UIScene), sceneWillEnterForeground(_ scene: UIScene), sceneDidEnterBackground(_ scene: UIScene)

The problem with these replacements is that they are specific to one of multiple scenes that are entering and leaving the foreground. They do not provide a simple and clean way to determine if the entire app is in the foreground or background.

Determining app foreground/background status is important for reasons that have nothing to do with the user interface. Some iOS features fail silently when the app is not in the foreground (wildcard bluetooth scanning and iBeacon transmission are two examples.) I often develop iOS frameworks that have no user interface whatsoever, so I need a way to determine app background/foreground state that does not rely on pasting a bunch of boilerplate code in the UIWindowSceneDelegate -- it is not reasonable for me to ask somebody using my framework to do that.

Are there any straightforward ways to determine the apps's foreground/background status on iOS 13 with SwiftUI?


Solution

  • You can use the UIApplication notifications in SwiftUI as well:

    • didEnterBackgroundNotification
    • willEnterForegroundNotification
    • didBecomeActiveNotification
    • willResignActiveNotification

    Here is an example:

    NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { _ in
        // active
    }
    
    NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: .main) { _ in
        // inactive
    }