---
schema_version: "1"
id: e7a3184790441684601f92b0e8d33f827e212667c8d812caee2ddb621efcc774
title: AgentforceUIDelegate
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/genai/agentforce-mobile-sdk/references/ios/agentforce-ui-delegate-ios.md
canonical_url: https://developer.salesforce.com/docs/ai/agentforce-mobile-sdk/references/agentforce-mobile-sdk/agentforce-ui-delegate-ios.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 85b65dedbcd55abd15a47387fb96898b8b6e89c6fa477d73883944546a2b4887
products:
  - Agentforce Mobile SDK
  - Agentforce and Einstein Generative AI
---
# AgentforceUIDelegate

## Overview

Protocol that enables customization of user interactions and conversation behavior within Agentforce UI components. Implement this protocol to intercept and modify utterances before sending, receive notifications about conversation events, and handle agent switching scenarios.

## Declaration

```swift
protocol AgentforceUIDelegate
```

## Methods

| Method                         | Signature                                                                                          | Description                                                                                                                    |
| ------------------------------ | -------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `modifyUtteranceBeforeSending` | `func modifyUtteranceBeforeSending(_ utterance: AgentforceUtterance) async -> AgentforceUtterance` | Allows modification of user utterances before they are sent to the agent. Returns the original utterance unchanged by default. |
| `didSendUtterance`             | `func didSendUtterance(_ utterance: AgentforceUtterance)`                                          | Called after an utterance has been successfully sent to the agent. No-op by default.                                           |
| `userDidSwitchAgents`          | `func userDidSwitchAgents(newConversation: AgentConversation)`                                     | Called when the user switches to a different agent during a conversation. No-op by default.                                    |
| `userInitiatedVoice`           | `func userInitiatedVoice(for conversation: AgentConversation)`                                     | Called when the user initiates a voice interaction for a conversation. No-op by default.                                       |

## Implementation Patterns

```swift
class MyUIDelegate: AgentforceUIDelegate {
    // Modify utterances before sending
    func modifyUtteranceBeforeSending(_ utterance: AgentforceUtterance) async -> AgentforceUtterance {
        // Add user context to the message
        var modifiedUtterance = utterance
        modifiedUtterance.context = [
            "userId": UserSession.current.userId,
            "timestamp": Date().iso8601
        ]
        return modifiedUtterance
    }
}
```

```swift
class AnalyticsUIDelegate: AgentforceUIDelegate {
    // Track when messages are sent
    func didSendUtterance(_ utterance: AgentforceUtterance) {
        Analytics.track("message_sent", properties: [
            "message_length": utterance.text.count,
            "has_attachments": !utterance.attachments.isEmpty
        ])
    }
}
```

```swift
class AgentSwitchDelegate: AgentforceUIDelegate {
    func userDidSwitchAgents(newConversation: AgentConversation) {
        NotificationCenter.default.post(
            name: .agentSwitched,
            object: nil,
            userInfo: ["conversation": newConversation]
        )
    }
}
```

## Switch From Chat View to Voice View

Use `userInitiatedVoice` to present your own instance of `AgentforceVoiceView`. `userInitiatedVoice` is called when a user taps the button that opens the voice view from the chat view.

This sample code demonstrates rerendering the view when a user selects the voice option.

```swift
enum PresentationState {
    case none
    case chat
    case voice
}
struct MyCustomView: View {
    @State var presentationState: PresentationState = .none
    var body: some View {
        // Show chatView and voiceView based on presentationState
    }
}
extension MyCustomView: AgentforceUIDelegate {
    func userInitiatedVoice(for conversation: any AgentConversation) {
        presentationState = .voice
    }
}
```
