Post

Replies

Boosts

Views

Activity

Reply to How to open a window in SwiftUI?
Tested on Xcode 13 beta, SwiftUI 3.0 After having being in this situation, I Frankensteined some answers that where all over the internet and this works for me: On the @main (MyAppApp) file add the amount of WindowGroup("Window Name") you need: import SwiftUI @main struct MyAppApp: App { var body: some Scene { WindowGroup { ContentView() } WindowGroup("Second Window") { SecondWindow() }.handlesExternalEvents(matching: Set(arrayLiteral: "SecondWindow")) WindowGroup("Third Window") { ThirdWindow() }.handlesExternalEvents(matching: Set(arrayLiteral: "ThirdWindow")) } What to place in every WindowGroup?: WindowGroup("SecondWindow") /*Any name you want to be displayed at the top of the window.*/ { SecondWindow() //View you want to display. }.handlesExternalEvents(matching: Set(arrayLiteral: "SecondWindow")) //Name of the view without (). Now, at the end of the MyAppApp file (outside of the struct MyAppApp: App) add the following enum : enum OpenWindows: String, CaseIterable { case SecondView = "SecondView" case ThirdView = "ThirdView" //As many views as you need. func open(){ if let url = URL(string: "myapp://\(self.rawValue)") { //replace myapp with your app's name NSWorkspace.shared.open(url) } } } Add the following to your Info.plist Replace myapp with your app's name. Usage: Button(action: { OpenWindows.SecondView.open() }){ Text("Open Second Window") }
Jul ’21