<!--
{
  "availability" : [
    "macOS: 27.0.0 -",
    "visionOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "SpatialPreview",
  "identifier" : "/documentation/SpatialPreview/DocumentPreviewSession",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Spatial Preview"
    ],
    "preciseIdentifier" : "s:14SpatialPreview08DocumentB7SessionC"
  },
  "title" : "DocumentPreviewSession"
}
-->

# DocumentPreviewSession

A session that streams document content to a connected visionOS device for spatial preview.

```
final class DocumentPreviewSession
```

## Overview

To start a `DocumentPreviewSession` preview, obtain an [`SpatialPreviewEndpoint`](/documentation/SpatialPreview/SpatialPreviewEndpoint) through one of two paths:

- Use [`ConnectedSpatialEndpointObserver`](/documentation/SpatialPreview/ConnectedSpatialEndpointObserver) when the visionOS device is already connected using Mac Virtual Display.
  Observe its [`isEndpointAvailable`](/documentation/SpatialPreview/ConnectedSpatialEndpointObserver/isEndpointAvailable) property to know when a device is ready, then access its [`endpoint`](/documentation/SpatialPreview/ConnectedSpatialEndpointObserver/endpoint) property to retrieve the endpoint.
- Use [`SpatialPreviewDevicePicker`](/documentation/SpatialPreview/SpatialPreviewDevicePicker), a <doc://com.apple.documentation/documentation/SwiftUI/View> that presents nearby companion devices and calls its closure with the chosen `SpatialPreviewEndpoint`, when you want to let someone select a device interactively.

When you have the endpoint, create a `DocumentPreviewSession`, the concrete session class that conforms to [`SpatialPreviewSession`](/documentation/SpatialPreview/SpatialPreviewSession) and <doc://com.apple.documentation/documentation/Swift/Sendable>.
Initialize the session with a display name and the <doc://com.apple.documentation/documentation/UniformTypeIdentifiers/UTType-swift.struct> of the content, then call [`start(endpoint:)`](/documentation/SpatialPreview/DocumentPreviewSession/start(endpoint:)) with the endpoint to open the connection:

```swift
let observer = ConnectedSpatialEndpointObserver()

// Wait for a device to become available.
let endpoint = try await observer.endpoint

// Create and start the session.
let session = DocumentPreviewSession(name: "My Spatial Content", contentType: .jpeg)

try await session.start(endpoint: endpoint)

// Push initial content, then update as the document changes.
let spatialContentURL = URL(filePath: "/path/to/spatialContent.jpeg")
try await session.updateContents(url: spatialContentURL)
```

After starting a session, use [`updateContents(data:)`](/documentation/SpatialPreview/DocumentPreviewSession/updateContents(data:)) to push a <doc://com.apple.documentation/documentation/Foundation/Data> value directly to the device; this is useful when your document content is already in memory.
Use [`updateContents(url:)`](/documentation/SpatialPreview/DocumentPreviewSession/updateContents(url:)) to stream content from a file URL, which avoids loading the entire file into memory at once.
Call either method each time the document changes.

Track connection changes using [`SpatialPreviewSessionState`](/documentation/SpatialPreview/SpatialPreviewSessionState).
When you finish, call [`close()`](/documentation/SpatialPreview/SpatialPreviewSession/close()) to end the session cleanly.

## Topics

### Initializers

[`convenience init(name: String, contentType: UTType)`](/documentation/SpatialPreview/DocumentPreviewSession/init(name:contentType:))

Create a new DocumentPreviewSession with the name and contentType, to be used for all content updates.

### Instance Properties

[`let contentType: UTType`](/documentation/SpatialPreview/DocumentPreviewSession/contentType)

The content type of the document. All updates must provide documents conforming to this content type.

[`let name: String`](/documentation/SpatialPreview/DocumentPreviewSession/name)

The display name of the document on the spatial preview

### Instance Methods

[`func start(endpoint: SpatialPreviewEndpoint) async throws`](/documentation/SpatialPreview/DocumentPreviewSession/start(endpoint:))

Connects to the specified endpoint and prepares the session to send document updates.

[`func updateContents(data: Data) async throws`](/documentation/SpatialPreview/DocumentPreviewSession/updateContents(data:))

Update the contents of the document with the provided data.

[`func updateContents(url: URL) async throws`](/documentation/SpatialPreview/DocumentPreviewSession/updateContents(url:))

Update the contents of the document with the provided URL.

## Relationships

### Conforms To

[`Copyable`](/documentation/Swift/Copyable)

[`SpatialPreviewSession`](/documentation/SpatialPreview/SpatialPreviewSession)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Escapable`](/documentation/Swift/Escapable)

[`Observable`](/documentation/Observation/Observable)

[`Sendable`](/documentation/Swift/Sendable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)