I have an app where I have a need where a button will open two windows. In the App struct I have the two window groups setup
@main
struct myApp: App {
var body : some Scene {
WindowGroup() {
WelcomeView()
}
WindowGroup("FirstViewGroup") {
FirstView()
}
.handlesExternalEvents(matching: Set(arrayLiteral: "FirstViewGroup"))
WindowGroup("SecondViewGroup") {
SecondView()
}
.handlesExternalEvents(matching: Set(arrayLiteral: "SecondViewGroup"))
}
}
In the WelcomeView I have the following button action:
Button {
if let firstURL = URL(string: "myApp://FirstViewGroup"), let secondURL = URL(string: "myApp://SecondViewGroup") {
NSWorkspace.shared.open(firstURL)
NSWorkspace.shared.open(secondURL)
}
} label: {
Text("Open Windows")
}
When I comment out one of the NSWorkspace.shared.open lines in the button action, the appropriate window opens. But when I have two sequential NSWorkspace.shared.open calls, I only get the first. Ideas? I have the URL Types in info.plist set correctly (since each window opens successfully when each is called individually)
CodePudding user response:
Try to open second URL in next event loop, like
Button {
if let firstURL = URL(string: "myApp://FirstViewGroup"), let secondURL = URL(string: "myApp://SecondViewGroup") {
NSWorkspace.shared.open(firstURL)
DispatchQueue.main.async {
NSWorkspace.shared.open(secondURL) // << here !!
}
}
} label: {
Text("Open Windows")
}