Skip to main content
The FishAgent Swift package adds voice calls with your agent to a native iOS app. It behaves like the Web SDK and the React Native SDK: the same session options, events, error codes, and end reasons. On top of the call, it handles the microphone permission and audio routing (loudspeaker, receiver, headsets). It has no UI. You build the screen, and the SDK gives you the state to show.
Calls, the microphone, audio routes, reconnection, and calls in the background and with the screen locked were checked on an iPhone 12. Headsets, phone-call interruptions, and moving between Wi-Fi and cellular have not been checked yet.

Authenticated sessions

Create session tokens on your backend.

Web SDK reference

Events, session options, and error codes.

Source and example app

The package and a one-screen example.

Requirements

  • iOS 15 or later.
  • Xcode 16.3 or later. The SDK is built on LiveKit’s Swift SDK, which needs Swift 6.1 tools.
  • Your own backend to create session tokens, or a public agent. Never ship an API key inside an app.
  • Tested with LiveKit’s Swift SDK 2.19.0 on an iPhone 12 with iOS 26.6 and an iPhone 16 simulator with iOS 18.5. Other versions may work but are untested.

Install

Add the package in Xcode (File > Add Package Dependencies) with the repository’s URL, or in Package.swift:
Package.swift
Add two things to your app’s Info.plist:
  • NSMicrophoneUsageDescription, the sentence iOS shows when it asks for the microphone. iOS ends an app that uses the microphone without it.
  • The audio background mode (UIBackgroundModes), so a call continues in the background and with the screen locked.

Quick start

CallView.swift
startSession asks for the microphone permission if the user has not been asked yet. The example app is one screen that does all of this.

Start a session

An app starts a call in one of two ways, the same two the browser SDKs offer. When the user starts a call, the app asks your backend for a session token. The backend calls POST /v1/agent/sessions with your API key and returns the response unchanged. The endpoint is the same one web apps use; see Authenticated sessions for backend examples. Pass the response body to the SDK without picking fields out of it:
Two things differ from the browser:
  • localhost is the phone, not your computer. During development, use your computer’s LAN address.
  • iOS blocks plain http:// unless the app allows it (NSAllowsLocalNetworking covers the local network). Serve the token endpoint over HTTPS in production.

Public agent

An agent with public access turned on takes sessions with just its id, with no API key and no backend:
If the agent has Restrict origins on, the platform checks the request’s Origin header against the agent’s allowed origins. An app sends none, so pass one that is on the list:
Without it, a restricted agent answers origin_forbidden.
The origin option is experimental and may change in a minor release: a dedicated way for apps to identify themselves may replace it. It is only visible to a file that imports the SDK as @_spi(Experimental) import FishAgent.
Anyone with the agent id can start sessions, billed to you. For a production app, especially one with signed-in users, prefer your backend: it decides who gets a token and can attribute sessions to your users. The React Native page says more about what a public agent exposes; all of it applies here.

Conversation

One object for your app’s calls, an ObservableObject on the main actor. It can make any number of calls, one at a time.
The first six are @Published, so SwiftUI views update with them. SessionOptions has the same fields as the Web SDK’s session options: origin, serverUrl, language, timezone, context, overrides, dynamicVariables, endUserId, metadata, toolEvents, clientTools, clientToolTimeout, and microphone. clientToolTimeout is a TimeInterval in seconds (15 by default) where the Web SDK counts milliseconds. ConversationOptions holds what the phone needs around calls: audioOutput. The same rules as the other SDKs apply:
  • A second startSession while one is on its way, or during a live call, joins that call.
  • endSession during a start stops it where it is, before the microphone is captured if the call had not got that far: startSession throws CancellationError, and the session, if one was created, is closed.
  • A failed start puts status back to idle.
  • sendUserMessage, sendUserActivity, and interrupt do nothing when there is no call.
  • The start belongs to the conversation. Cancelling the task that called startSession (a SwiftUI .task whose view went away, for example) stops the wait, not the call. Call endSession to hang up. Releasing the Conversation also ends its call.

Events

events() carries the same events as the Web SDK’s event reference, as subclasses of ConversationEvent:
Each call to events() makes a new stream, so several parts of your app can listen. Nothing is replayed: an event from before the stream was made is gone, so read state from the published properties. A consumer that falls more than 256 events behind loses the oldest ones. The streams end when the Conversation is released.

One call per app

Only one call can run in a process at a time, whichever Conversation makes it. A second conversation’s startSession throws ConversationUsageError.callAlreadyActive until the first call has ended. The audio session is shared by the whole process.

Compatibility

What can gain members in a minor release is shaped so that your code already handles a member it does not know.
  • Events. ConversationEvent is a class, and each kind of event is a subclass (ConversationEvent.Message, ConversationEvent.ToolCallStarted, and so on). A switch over events matches with as and has a default. An event can also gain a property.
  • End reasons, error codes, error stages, tool sources, and audio routes. EndReason, FishAgentError.Code, FishAgentError.Stage, ToolCallSource, and AudioOutputRoute are structs with static members, not enums. Compare with the members (reason == .userHangup) and keep a default. Their rawValue is the text the API and the other SDKs use ("user_hangup"), which is also how this page writes them.
ConversationStatus, AgentMode, and MessageRole are enums. They are closed sets, and a minor release adds no case to them, so a switch over one of them needs no default. A session token shows only its sessionId. The rest of it is how the SDK joins the call.

What is left out

As in the React Native SDK, the browser-only members are not there: startAudio, setOutputVolume, setInputDevice, setOutputDevice, audio levels, and the screen wake lock. There is also no getRoom(): no LiveKit type appears in the SDK’s API, so your app can move to a newer LiveKit 2.x without the SDK changing.

Client tools

A client tool is an async closure from the arguments to a result:
Arguments and results are JSONValue, which can be written with literals. Returning nil sends no result. Throwing reports a failure to the agent. Handlers do not run on the main actor; mark the closure @MainActor to touch the UI.

Audio

LiveKit configures, activates, and deactivates your app’s audio session by itself as a call’s audio starts and stops. The SDK does not touch activation. It states a route preference, and when the call ends it puts the session’s category, mode, and options back to what your app had before the call. LiveKit leaves the call category behind, and other audio in your app would otherwise play quieter and ignore the silent switch. During a call the session is playAndRecord, in video-chat mode on the speaker and voice-chat mode on the receiver. If your app plays other audio, expect its audio session to be reconfigured for the length of a call. The SDK does not activate the session again afterwards: an app that was playing starts its playback again itself.

Troubleshooting

Going further

React Native SDK

The same call in a React Native or Expo app.

Client tools

Let the agent call functions in your app.

Authenticated sessions

Create session tokens on your backend.

Wire protocol

What the SDK speaks underneath.