0

I'm using SwiftUI with Big Sur and the life cycle of SwiftUI (not AppDelegate):

@main
struct MyApp: App {
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

In this case, I can open multiple windows with cmd+n. How can I find out, which window is the one that is focused?

Lupurus
  • 3,618
  • 2
  • 29
  • 59
  • 1
    You can access own window as shown in https://stackoverflow.com/a/63439982/12299030 and ask if it is key window. But it would be better if you describe what do you really want to do, NSWindow might be not needed for that, you know. – Asperi Nov 24 '20 at 18:25
  • Thanks, I will have a look at that. My goal is: I have a shared class that handles network stuff. In some cases there should be displayed a sheet or an alert and it should only be displayed on the main and active window, not on every window. – Lupurus Nov 24 '20 at 18:36
  • @Asperi: Thank you so much, it works perfectly. I used this code also for this problem: https://stackoverflow.com/questions/64993265/swiftui-and-appkit-use-close-dialog-to-ask-if-the-app-is-allowed-to-quit/64993266 – Lupurus Nov 24 '20 at 19:10

0 Answers0