In the AppKit lifecycle it was possible to open new windows by calling showWindow(_ sender: Any?) on an NSWindowController instance, whether it was initialized via a storyboard or via code alone.
I see that on macOS in the new SwiftUI Lifecycle you can create a new window by default using ⌘N, but it's a new copy of the existing window. Can we declare a completely unique scene and open it in a new window?
I saw that it was possible to declare custom scenes, but how do we invoke them if they aren't the default scene at startup?
Thanks!
I see that on macOS in the new SwiftUI Lifecycle you can create a new window by default using ⌘N, but it's a new copy of the existing window. Can we declare a completely unique scene and open it in a new window?
I saw that it was possible to declare custom scenes, but how do we invoke them if they aren't the default scene at startup?
Thanks!
Here is how to open a new window in SwiftUI on macOS.
In your ContentView create a button and open a URL for your app and another View e.g. Viewer to be shown in the window we will open:
In your App add another WindowGroup for your viewer and set it to enable handling of external launch events (an internal event in our case).
Now in Project->Info->URL Types type in myappname in the URL Schemes field (and the identifier field too) to register our app with the system.
Now run your app and click the button and it should open a new window!
In your ContentView create a button and open a URL for your app and another View e.g. Viewer to be shown in the window we will open:
Code Block struct ContentView: View { @Environment(\.openURL) var openURL var body: some View { VStack { Button("Open Viewer") { if let url = URL(string: "myappname://viewer") { openURL(url) } } Text("Hello, world!") } .padding() } } struct Viewer: View { var body: some View { Text("Viewer") } }
In your App add another WindowGroup for your viewer and set it to enable handling of external launch events (an internal event in our case).
Code Block @main struct GroupDefaultsTestApp: App { var body: some Scene { WindowGroup { ContentView() } WindowGroup("Viewer") { // other scene Viewer() } .handlesExternalEvents(matching: Set(arrayLiteral: "*")) } }
Now in Project->Info->URL Types type in myappname in the URL Schemes field (and the identifier field too) to register our app with the system.
Now run your app and click the button and it should open a new window!