Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
1.5 KB
Raw Blame
diff --git a/ios/Utilities/Utilities.swift b/ios/Utilities/Utilities.swift
index 3144ea069305fc36a8ffc2cd9f3fdff9b21257a9..ada48093d9bf3a2e3f1cca5adb7d4c4238ab5005 100644
--- a/ios/Utilities/Utilities.swift
+++ b/ios/Utilities/Utilities.swift
@@ -138,10 +138,23 @@ public struct Utilities {
     return convertToUrl(string: string)
   }
 
+  /**
+   Returns the app's key window. On iOS and tvOS it is resolved from the foregrounded scene, so
+   callers presenting UI don't reach a window in a scene the user isn't looking at.
+   */
+  @MainActor
+  public static func keyWindow() -> UIWindow? {
+#if os(iOS) || os(tvOS)
+    return SceneGeometry.keyWindow()
+#elseif os(macOS)
+    return NSApplication.shared.keyWindow
+#endif
+  }
+
   nonisolated public func currentViewController() -> UIViewController? {
     return MainActor.assumeIsolated {
 #if os(iOS) || os(tvOS)
-      var controller = UIApplication.shared.keyWindow?.rootViewController
+      var controller = Utilities.keyWindow()?.rootViewController
 
       while let presentedController = controller?.presentedViewController, !presentedController.isBeingDismissed {
         controller = presentedController
@@ -149,7 +162,7 @@ public struct Utilities {
       return controller
 #elseif os(macOS)
       // Even though the function's return type is `UIViewController`, react-native-macos will alias `NSViewController` to `UIViewController`.
-      return NSApplication.shared.keyWindow?.contentViewController
+      return Utilities.keyWindow()?.contentViewController
 #endif
     }
   }