Here is how to open a new window or re-activate an existing one 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:

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).

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

        WindowGroup("Viewer") { // other scene
            Viewer().handlesExternalEvents(preferring: Set(arrayLiteral: "viewer"), allowing: Set(arrayLiteral: "*")) // activate existing window if exists
        }
        .handlesExternalEvents(matching: Set(arrayLiteral: "viewer")) // create new window if one doesn't exist
    }
}

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 first window click the button again and it’ll re-activate the existing second window bringing it to front.

Categories: SwiftUI