2

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
            }
        }
    }
}
Nimbahus
  • 477
  • 3
  • 16
  • The unwanted side effect is basically any small interference puts the app into "inactive" and then the secure window pops up for a split second. You could apply it after waiting for some time, but this introduces further issues. The reachability swipe gesture is explained by Apple [here](https://support.apple.com/guide/iphone/reachability-iph145eba8e9/ios) – Nimbahus Jan 24 '23 at 15:19
  • I just tested your example, and for me, it works perfectly fine if I uncomment `//showSecureWindow = true` in the `.inactive` case. Nothing flickers when I do the reachability swipe, but as soon as I start to change apps, I get the `EmptyView` – Lasse Jan 25 '23 at 16:54
  • Maybe to make it a little bit more clear, change the EmptyView to a Rectangle with a blue color filling the whole screen, and then do the reachability swipe. You will see the blue rectangle for a split second when doing the reachability swipe. This will be the case for most "secure windows", as we tend to display our app names or a background color. – Nimbahus Jan 25 '23 at 17:52

0 Answers0