# Work with multiple documents in AI Assistant

### SwiftUI

[SwiftUI](https://www.nutrient.io/guides/ios/ai-assistant/multiple-documents-swiftui.md)

### UIKit

[UIKit](https://www.nutrient.io/guides/ios/ai-assistant/multiple-documents-uikit.md)

> Note: This guide only applies when connecting Nutrient iOS SDK to [AI Assistant](https://www.nutrient.io/guides/ai-assistant.md) running on a server. Multiple documents aren’t supported when using the [on-device AI Assistant](https://www.nutrient.io/guides/ios/ai-assistant/on-device.md) feature of Nutrient iOS SDK.

This guide shows how to use AI Assistant with multiple documents, enabling users to ask questions and get insights across their entire document collection using [`AIAssistantViewController`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/aiassistantviewcontroller).

When working with multiple documents, AI Assistant enables users to:

- Ask questions that span across multiple documents in your app

- Get contextual answers that reference specific content from any document

- Navigate seamlessly between documents based on links

Working with multiple documents is particularly powerful for apps that handle document workflows, research materials, or any scenario where users require the content of multiple related documents simultaneously.

Before starting, ensure you have a working [setup running AI Assistant](https://www.nutrient.io/sdk/ai-assistant/getting-started/ios.md) for your project.

Using AI Assistant with multiple documents requires the multiple documents license component for AI Assistant, which was added in the [AI Assistant](https://www.nutrient.io/guides/ai-assistant.md) 1.5 release. Contact our [Support team](https://support.nutrient.io/hc/en-us/requests/new) for more information.

## Creating the configuration

To support multiple documents, create an [`AIAssistantConfiguration`](https://www.nutrient.io/api/ios/documentation/pspdfkit/aiassistantconfiguration) that includes all document IDs in its JSON Web Token (JWT). This token authorizes the session and tells the AI Assistant server which documents are available for the session. You can read more about JWTs and supported claims in the [generate a JWT](https://www.nutrient.io/guides/ai-assistant/viewer-integration/client-authentication/generate-a-jwt.md) guide.

```swift

func createAIAssistantConfiguration(for documents: [Document]) -> AIAssistantConfiguration {
    let sessionID = "multi-document-ios-session"

    let claims: [String: Any] = [
        "document_ids": documents.compactMap { $0.documentId?.hexadecimalEncodedString() },
        "session_ids": [sessionID]
    ]

    // In production, generate a JWT server-side for security.
    let jwt = generateJWT(claims: claims)

    // Use the server URL where your AI Assistant is hosted.
    let serverURL = URL(string: "http://localhost:4000")!

    return AIAssistantConfiguration(serverURL: serverURL, jwt: jwt, sessionID: sessionID)
}

```

## Presenting the UI

Create an [`AIAssistantSession`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/aiassistantsession) with the document collection you want to make available to AI Assistant and your configuration, and use it to create [`AIAssistantViewController`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/aiassistantviewcontroller) and present the view controller:

```swift

func setupAIAssistant(with documents: [Document]) -> AIAssistantViewController {
    let configuration = createAIAssistantConfiguration(for: documents)

    let session = AIAssistantSession(documents: documents, configuration: configuration)
    let aiAssistantViewController = AIAssistantViewController(session: session)

    aiAssistantViewController.delegate = self
    return aiAssistantViewController
}

```

[`AIAssistantViewController`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/aiassistantviewcontroller) handles setting up the connection to AI Assistant and shows the chat interface to start interacting with the documents once it’s ready.

## Handling document navigation

Implement [`AIAssistantViewControllerDelegate`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/aiassistantviewcontrollerdelegate) to handle navigation when AI Assistant directs users to specific content. This delegate method is called whenever AI Assistant wants to show the user relevant information in a particular document:

```swift

func aiAssistantViewController(_ aiAssistantViewController: AIAssistantViewController, navigateTo document: Document, pageIndex: PageIndex, rects: [CGRect]) {
    // Switch to the correct document in your interface.
    switchToDocument(document)

    // Navigate to the specific page and highlight relevant areas.
    navigateToPage(pageIndex, highlightingRects: rects)
}

```

The `rects` parameter contains the specific areas on the page that AI Assistant wants to highlight in PDF coordinate space.

If you’re using [`PDFViewController`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/pdfviewcontroller) to display documents, you can use the [`setPageIndexWithHighlights(_:_:)`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/pdfviewcontroller/setpageindexwithhighlights(_:_:)) API to navigate to the page and highlight the relevant areas:

```swift

func navigateToPage(_ pageIndex: PageIndex, rects: [CGRect]) {
    // Navigate to and highlight the specific locations.
    pdfController.setPageIndexWithHighlights(pageIndex, rects)
}

```

## Integration patterns

For projects using [`PDFTabbedViewController`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/pdftabbedviewcontroller), integrate AI Assistant to work seamlessly with your existing document tabs. You can make the relevant document tab automatically visible when navigating to content from separate documents:

```swift

func aiAssistantViewController(_ aiAssistantViewController: AIAssistantViewController, navigateTo document: Document, pageIndex: PageIndex, rects: [CGRect]) {
    // Switch to the document tab.
    tabbedViewController.setVisibleDocument(document, scrollToPosition: true, animated: true)

    // Navigate to and highlight the specific locations.
    if let pdfController = tabbedViewController.pdfController {
        pdfController.setPageIndexWithHighlights(pageIndex, highlights: rects)
    }
}

```

## Managing document changes

When your document collection changes, you need to recreate the [`AIAssistantViewController`](https://www.nutrient.io/api/ios/documentation/pspdfkitui/aiassistantviewcontroller) with an updated JWT that reflects the new document set. This ensures AI Assistant has access to the correct documents:

```swift

func addDocument(_ document: Document) {
    // Remove existing AI Assistant.
    aiAssistantViewController?.removeFromParent()
    aiAssistantViewController = nil

    // Recreate with updated document list.
    let aiAssistantViewController = setupAIAssistant(with: documents)
}

```

## Example integrations

Take a look at [`MultiDocumentAIAssistantExample`](https://github.com/PSPDFKit/pspdfkit-ios-catalog/blob/master/Catalog/Examples/Top/AIAssistantExample/MultiDocumentAIAssistantExample.swift) in our Catalog project for an example of how you can use multiple documents with AI Assistant.
---

## Related pages

- [Customize AI Assistant appearance](/guides/ios/ai-assistant/appearance-customization-swiftui.md)
- [Customize AI Assistant appearance](/guides/ios/ai-assistant/appearance-customization-uikit.md)
- [Add AI capabilities to Nutrient document viewer](/guides/ios/ai-assistant.md)
- [Work with multiple documents in AI Assistant](/guides/ios/ai-assistant/multiple-documents-swiftui.md)
- [Introduction to AI Assistant](/guides/ios/ai-assistant/introduction.md)
- [On-device AI Assistant with Apple Intelligence](/guides/ios/ai-assistant/on-device.md)

