3

How do I restrict an application to only one window in SwiftUI for macOS? Many Apple applications such as the Notes and the Developer app do not allow creation of another Window. I am wondering how to go about implementing similar behavior in SwiftUI.

I found that this behavior is allowed by default. Essentially, I am trying to eliminate this option here for New Window:

enter image description here

0

3 Answers 3

4

To do this, you need to change the Scene your app uses. WindowGroup implements a multi-window interface, so as long as you're using WindowGroup, you'll have several app behaviors that are associated with a multi-window app.

Use Window instead.

I wrote a blog post about it at https://www.optionalmap.com/posts/swiftui_single_window_app/

Sign up to request clarification or add additional context in comments.

3 Comments

This worked great for me! And interestingly, on Ventura 13.4, it was enough to use Window instead of WindowGroup – it automatically removed the tab-related menu items for me.
It would be great to have a single-window solution that still works with macOS 12 and WindowGroup.
Cool solution but I need macOS 12 support as well.
3

I discovered a way to get rid of the option to create a new window using the .commands modifier in your App struct

 var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .commands {
            CommandGroup(replacing: CommandGroupPlacement.newItem) {
                EmptyView()
            }
        }
    }

1 Comment

Eddy's comment below unfortunately is relevant if you ever want to put your app into the App Store. The menu item should be removed conditionally whether there is an open window or not. I've yet to found a way to achieve that.
2

The answer above will work but if you haven't got any workarounds in place app store will reject it because there won't be any possibility for the user to create a new window when closing the previous one. So in order to get your app to work again the user needs to quit the app completely and reopen again, which is against Apple's macOS human interface guidelines

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.