TransWikia.com

Determine background status with SwiftUI

Stack Overflow Asked by davidgyoung on February 28, 2021

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?

2 Answers

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
}

Correct answer by pawello2222 on February 28, 2021

I believe the correct way to handle events in SwiftUI is to use the onReceive method.

Text("Hello, World!")
    .onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in
        // Action
    }

Answered by Iaenhaall on February 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP