I'm opening an immersive space:
Button {
Task{
await openImmersiveSpace(id: "globe")
}
} label: {
Text("Jump to Globe")
}
If I click multiple times I get a warning: Unable to present another Immersive Space when one is already requested or connected
The solution I can think of at present is to create an environment variable to save the state of entering the immersive space, and determine whether it has been entered when the button is clicked.
Globe()
.onAppear{
model.isShowGlobe = true
}
.onDisappear{
model.isShowGlobe = false
}
Button {
if model.isShowGlobe{
return
}
Task{
await openImmersiveSpace(id: "globe")
}
} label: {
Text("Jump to Globe")
}
Or after I enter the immersive space from the Globe
, hide or close the Globe
. Do you have any better suggestions?
At first, I should say that a visionOS app can display just ONE space at a time. You'll get an error if you try to open a new space while the previous one is visible. To dismiss an open space, use the code below. There's no need to specify an id when dismissing your immersive space because there can only be just one space open at a time.
However, the most robust approach in your case is to have just one button, switching it from the
True
state to theFalse
state. This is what it looks like:P. S.
Also, the
Unable to present another Immersive Space when one is already requested or connected
error appears in cases where you simultaneously call Immersion Space from the ContentView and from the App files.