1

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?

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Does `NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { _ in` not work for you? – pawello2222 Sep 09 '20 at 19:31
  • 1
    @pawello222, I stand corrected. Contrary to what I say in my question, the four notifications I say do not fire still do fire. I was certain last time I tried that didn't happen, but apparently I was wrong. If you make this an answer I will gladly accept. – davidgyoung Sep 09 '20 at 21:01
  • in the WWDC19 [Introducing Multiple Windows on iPad](https://developer.apple.com/videos/play/wwdc2019/212/) there is an explanation of this possible "discrepancy"; right around 17:06 there is a chart but the whole talk is very informative – lorem ipsum Feb 19 '21 at 13:25

3 Answers3

5

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
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
3

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
    }
Iaenhaall
  • 404
  • 5
  • 9
  • 2
    While the above may work great for a specific scene, the question is about how do determine the foreground/background state for the entire application across many scenes – davidgyoung Feb 18 '21 at 16:45
3

The correct way is not to use NotificationCenter at all but rather use the “scenePhase” Environment object. Can be used in any SwiftUI view that needs to see the state or the entire app.

@main

struct ExampleApp: App {
    
    @Environment(\.scenePhase) var scenePhase
    
    var body: some Scene {
        WindowGroup {
            MainView()
                .onChange(of: scenePhase) { phase in
                    switch phase {
                    case .background:
                        print("App is background")
                    case .inactive:
                        print("App is inactive")
                    case .active:
                        print("App is active")
                    @unknown default:
                        print("Phase is unknown")
                    }
                }
        }
    }
}
Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
Hajji Daoud
  • 157
  • 1
  • 8