1 min read

Immersive scene phases

Photo of an abandoned french theater
Photo by v2osk / Unsplash

Observing the ScenePhase in the environment has demonstrated to be a far more reliable technique for determining if an ImmersiveSpace is active or not. This is particularly helpful for making accessibility announcements, as it allows the user to adjust to the context switch.

import SwiftUI

@main
struct ImmersiveScenePhaseApp: App {
  @Environment(\.scenePhase) var scenePhase

  var body: some Scene {
    WindowGroup {
      ContentView()
    }

    ImmersiveSpace(id: "ImmersiveSpace") {
      ImmersiveView()
    }
    .onChange(of: scenePhase) { _, newPhase in
      var scenePhaseDescription = AttributedString("ImmersiveSpace \(newPhase)")
      scenePhaseDescription.accessibilitySpeechAnnouncementPriority = .high

      switch newPhase {
        case .active:
          AccessibilityNotification.Announcement(scenePhaseDescription).post()

        case .inactive:
          AccessibilityNotification.Announcement(scenePhaseDescription).post()

        default:
          break
      }
    }
  }
}

Basic demonstration built on top of the basic Xcode template

🙏
Thank you @Harlan for pointing this up