I am currently implementing a secure window which blocks the View if the app is in the background or shown in the app switcher. However, if the scenePhase is set to background, the View doesn't change in the app switcher. If the View is changed when the scenePhase goes to inactive, the changes are made visible in the app switcher, however, we get unwanted behavior if e.g. the user uses the reachability swipe, the secure window pops in and out. I tried to implement it with a SceneDelegate, but nothing changes in the behavior.
Is there any way to apply UI changes when the scenePhase hits background? I saw this thread here asking the same thing, but apparently it was a bug back then.
Sample Code:
import SwiftUI
@main
struct SwiftUIView: App {
@State private var showSecureWindow: Bool = false
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
if showSecureWindow {
EmptyView()
} else {
Text("Hello")
}
}
.onChange(of: scenePhase) { newScenePhase in
switch newScenePhase {
case .active:
showSecureWindow = false
case .inactive:
//showSecureWindow = true // <- This works, but unwanted behavior
break
case .background:
showSecureWindow = true // <- This doesn't work
@unknown default:
break
}
}
}
}