|
| 1 | +# Immersive Spaces |
| 2 | + |
| 3 | +One of the key features of visionOS are [`ImmersiveSpaces`](https://developer.apple.com/documentation/swiftui/immersive-spaces), which allow to display unbounded content in a person’s surroundings. |
| 4 | + |
| 5 | +:::warning |
| 6 | +Make sure to set `UIApplicationSupportsMultipleScenes` to `true` in `Info.plist` as described [here](/api/XR#uiapplicationsupportsmultiplescenes). |
| 7 | +::: |
| 8 | + |
| 9 | +### Declare ImmersiveSpace in `App.swift` |
| 10 | + |
| 11 | +```swift title="App.swift" |
| 12 | +@main |
| 13 | +struct HelloWorldApp: App { |
| 14 | + @UIApplicationDelegateAdaptor var delegate: AppDelegate |
| 15 | + // highlight-next-line |
| 16 | + @State private var immersionLevel: ImmersionStyle = .mixed |
| 17 | + |
| 18 | + var body: some Scene { |
| 19 | + RCTMainWindow(moduleName: "HelloWorldApp") |
| 20 | +// highlight-start |
| 21 | + ImmersiveSpace(id: "TestImmersiveSpace") { |
| 22 | + // RealityKit content goes here |
| 23 | + } |
| 24 | + .immersionStyle(selection: $immersionLevel, in: .mixed, .progressive, .full) |
| 25 | +// highlight-end |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +ImmersiveSpaces can have multiple levels of immersion: |
| 31 | + |
| 32 | +- The mixed style blends your content with passthrough. |
| 33 | +- The full style displays only your content, with passthrough turned off. |
| 34 | +- The progressive style completely replaces passthrough in a portion of the display. |
| 35 | + |
| 36 | +For more information about `ImmersiveSpace` API refer to [Apple documentation](https://developer.apple.com/documentation/swiftui/immersive-spaces). |
| 37 | + |
| 38 | +### Open new ImmersiveSpace from JS |
| 39 | + |
| 40 | +In order to open a new ImmersiveSpace, call `XR.requestSession()`. |
| 41 | + |
| 42 | +```ts |
| 43 | +import { XR } from "@callstack/react-native-visionos"; |
| 44 | +//... |
| 45 | +const openXRSession = async () => { |
| 46 | + try { |
| 47 | + if (!XR.supportsMultipleScenes) { |
| 48 | + Alert.alert("Error", "Multiple scenes are not supported"); |
| 49 | + return; |
| 50 | + } |
| 51 | + // highlight-next-line |
| 52 | + await XR.requestSession("TestImmersiveSpace"); // Pass the same identifier from `App.swift` |
| 53 | + } catch (e) { |
| 54 | + Alert.alert("Error", e.message); |
| 55 | + } |
| 56 | +}; |
| 57 | +``` |
| 58 | + |
| 59 | +:::warning |
| 60 | + |
| 61 | +Opening an `ImmersiveSpace` can fail in following scenarios: |
| 62 | + |
| 63 | +- `ImmersiveSpace` is not declared. |
| 64 | +- `UIApplicationSupportsMultipleScenes` is set to `false`. |
| 65 | +- User cancels the request. |
| 66 | + |
| 67 | +::: |
| 68 | + |
| 69 | +This will open a new immersive space for the user. Later on if you want to close it, call: |
| 70 | + |
| 71 | +```ts |
| 72 | +const closeXRSession = async () => { |
| 73 | + // highlight-next-line |
| 74 | + await XR.endSession(); |
| 75 | +}; |
| 76 | +``` |
| 77 | + |
| 78 | +:::note |
| 79 | + |
| 80 | +Only one immersive space can be open at a time. |
| 81 | + |
| 82 | +::: |
0 commit comments