Bruce Webster
Bruce Webster

Reputation: 541

How to tell if app entered background on iOS14+ SwiftUI?

I'm writing an iOS14+ app, trying to use .onChange(of: scenePhase) which supposedly is the modern replacement for the app entering foreground or background.

But I receive the .inactive phase while a system dialog pops up (e.g. when asking for pairing a device) - that is not the same thing as the old didEnterBackground/foreground!

I currently do not do any background processing, so I do not receive a .background phase -- only .inactive and then .active again. So there’s no easy way to tell the difference between the system dialog and actually going into the background.

I want to know if the user has left my app and come back. Is that not available going forward? I don't want to use old deprecated methods.

Upvotes: 7

Views: 3799

Answers (1)

Bruce Webster
Bruce Webster

Reputation: 541

This does the trick.

.onReceive(NotificationCenter.default.publisher(
    for: UIScene.willEnterForegroundNotification)) { _ in 
…
}

Upvotes: 16

Related Questions