> ## Documentation Index
> Fetch the complete documentation index at: https://humain.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# iOS

> Integrate Humain Kiosk into an iOS app — text chat, streaming, and real-time voice with SwiftUI.

The iOS SDK provides native Swift async/await APIs for text sessions and a full WebRTC voice
pipeline with `AVAudioSession` integration. The underlying WebRTC protocol is described in the
[WebRTC connection guide](https://humain.mintlify.site/connections/webrtc-voice.md) — this page covers the iOS-specific
implementation.

> [!TIP]
>
> During development, initialise the client with an `hk_test_…` sandbox credential instead of
> `hk_live_…`. There is no separate sandbox mode or URL — sandbox behavior comes entirely from
> the credential prefix. No AI credits are consumed and no usage is billed. See
> [Sandbox mode](https://humain.mintlify.site/concepts/sandbox-mode.md).

***

## Install

**1. Add the Swift package**

In Xcode, open your project and select **File → Add Package Dependencies**.

Enter the repository URL:

```text theme={null}
https://github.com/humain-ai/kiosk-swift
```

Select **Up to Next Major Version** from `1.0.0` and click **Add Package**.

> [accordion] Screenshot — Swift Package Manager dialog
>
> Screenshot: Xcode → File → Add Package Dependencies → kiosk-swift

**2. Add the package to your target**

In the package selection dialog, check the box next to your app target, then click
**Add Package**.

**3. Add the microphone usage description**

Open `Info.plist` (or your target's **Info** tab in Xcode) and add:

```xml theme={null}
<key>NSMicrophoneUsageDescription</key>
<string>This app uses your microphone to talk with the AI assistant.</string>
```

This key is **required** for voice sessions. The app will crash on launch without it.

***

## Text and streaming

> [tab] Text session
>
> Each `openSession()` call creates an independent conversation on the server. Use `defer`
> to ensure the session is always closed.
>
> ```swift theme={null}
> import HumainKiosk
> import SwiftUI
>
> class ChatViewModel: ObservableObject {
>     private let kiosk = KioskClient(credential: "hk_live_your_credential_here")
>
>     @Published var response: String = ""
>     @Published var isLoading: Bool = false
>
>     func sendMessage(_ text: String) async {
>         isLoading = true
>         defer { isLoading = false }
>
>         do {
>             let session = try await kiosk.openSession()
>             defer { Task { try? await session.close() } }
>
>             let result = try await session.sendMessage(text)
>             await MainActor.run { self.response = result.text }
>         } catch {
>             await MainActor.run { self.response = "Error: \(error.localizedDescription)" }
>         }
>     }
> }
> ```

> [tab] Streaming
>
> `session.stream()` returns an `AsyncThrowingStream<StreamEvent, Error>`. Use `for try await`
> to consume tokens as they arrive.
>
> ```swift theme={null}
> import HumainKiosk
> import SwiftUI
>
> class ChatViewModel: ObservableObject {
>     private let kiosk = KioskClient(credential: "hk_live_your_credential_here")
>
>     @Published var streamedText: String = ""
>
>     func streamMessage(_ text: String) async {
>         do {
>             let session = try await kiosk.openSession()
>             defer { Task { try? await session.close() } }
>
>             var accumulated = ""
>             for try await event in session.stream(text) {
>                 if case .chunk(let content) = event {
>                     accumulated += content
>                     await MainActor.run { self.streamedText = accumulated }
>                 }
>             }
>         } catch {
>             await MainActor.run {
>                 self.streamedText = "Error: \(error.localizedDescription)"
>             }
>         }
>     }
> }
> ```
>
> Stream events:
>
> | Case | Associated value | Description |
> | - | - | - |
> | `.turnStart` | — | AI has started generating |
> | `.chunk(String)` | token fragment | Append to output buffer |
> | `.toolCall(name:callId:)` | names | Action endpoint invoked |
> | `.toolResult(callId:status:)` | status | Action returned |
> | `.turnEnd(messageCount:)` | count | Generation complete |

***

## Voice session

> [!NOTE]
>
> The SDK requests microphone permission automatically on the first `openVoiceSession()` call.
> The `NSMicrophoneUsageDescription` key in `Info.plist` **must** be set or the app will crash
> before showing the permission prompt.

```swift theme={null}
import HumainKiosk
import AVFoundation
import SwiftUI

@MainActor
class VoiceViewModel: NSObject, ObservableObject, KioskVoiceSessionDelegate {
    private let kiosk = KioskClient(credential: "hk_live_your_credential_here")
    private var voiceSession: VoiceSession?

    @Published var status: VoiceStatus = .idle
    @Published var transcript: String = ""
    @Published var lastResponse: String = ""

    func startSession() async throws {
        // Configure AVAudioSession for VoIP-quality audio
        let audioSession = AVAudioSession.sharedInstance()
        try audioSession.setCategory(
            .playAndRecord,
            mode: .voiceChat,
            options: [.defaultToSpeaker, .allowBluetooth]
        )
        try audioSession.setActive(true)

        // Register for interruption and route-change notifications
        registerAudioSessionObservers()

        voiceSession = try await kiosk.openVoiceSession(delegate: self)
        try await voiceSession?.start()
    }

    func stopSession() async throws {
        try await voiceSession?.stop()
        voiceSession = nil
        removeAudioSessionObservers()
        try AVAudioSession.sharedInstance().setActive(
            false,
            options: .notifyOthersOnDeactivation
        )
        status = .idle
    }

    // MARK: — KioskVoiceSessionDelegate

    func session(_ session: VoiceSession, didTranscribe text: String) {
        transcript = "You: \(text)"
    }

    func session(_ session: VoiceSession, didReceiveResponse text: String) {
        lastResponse = text
    }

    func session(_ session: VoiceSession, didChangeStatus newStatus: VoiceStatus) {
        status = newStatus
    }

    func session(_ session: VoiceSession, didFailWith error: KioskError) {
        print("Voice session error: \(error)")
        status = .idle
    }
}
```

***

### Handling interruptions (phone calls)

> [accordion] Pause the voice session when a call or alarm interrupts
>
> Observe `AVAudioSession.interruptionNotification` and pause/resume the session accordingly.
>
> ```swift theme={null}
> // MARK: — AVAudioSession notification observers
>
> private func registerAudioSessionObservers() {
>     NotificationCenter.default.addObserver(
>         self,
>         selector: #selector(handleInterruption),
>         name: AVAudioSession.interruptionNotification,
>         object: AVAudioSession.sharedInstance()
>     )
>     NotificationCenter.default.addObserver(
>         self,
>         selector: #selector(handleRouteChange),
>         name: AVAudioSession.routeChangeNotification,
>         object: AVAudioSession.sharedInstance()
>     )
> }
>
> private func removeAudioSessionObservers() {
>     NotificationCenter.default.removeObserver(
>         self,
>         name: AVAudioSession.interruptionNotification,
>         object: nil
>     )
>     NotificationCenter.default.removeObserver(
>         self,
>         name: AVAudioSession.routeChangeNotification,
>         object: nil
>     )
> }
>
> @objc private func handleInterruption(_ notification: Notification) {
>     guard let info = notification.userInfo,
>           let typeValue = info[AVAudioSessionInterruptionTypeKey] as? UInt,
>           let type = AVAudioSession.InterruptionType(rawValue: typeValue)
>     else { return }
>
>     switch type {
>     case .began:
>         // Phone call, Siri, or alarm started — mute our session
>         voiceSession?.isMuted = true
>
>     case .ended:
>         guard let optionsValue = info[AVAudioSessionInterruptionOptionKey] as? UInt else { return }
>         let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
>         if options.contains(.shouldResume) {
>             // Reactivate and unmute
>             try? AVAudioSession.sharedInstance().setActive(true)
>             voiceSession?.isMuted = false
>         }
>
>     @unknown default:
>         break
>     }
> }
> ```

### Handling route changes (headphones)

> [accordion] Respond to audio route changes — headphone plug/unplug
>
> When the user unplugs headphones, iOS routes audio to the speaker, which can cause echo.
> Pause the session briefly while the route stabilises.
>
> ```swift theme={null}
> @objc private func handleRouteChange(_ notification: Notification) {
>     guard let info = notification.userInfo,
>           let reasonValue = info[AVAudioSessionRouteChangeReasonKey] as? UInt,
>           let reason = AVAudioSession.RouteChangeReason(rawValue: reasonValue)
>     else { return }
>
>     switch reason {
>     case .oldDeviceUnavailable:
>         // Headphones unplugged — mute briefly to prevent echo burst
>         voiceSession?.isMuted = true
>         DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
>             self?.voiceSession?.isMuted = false
>         }
>
>     case .newDeviceAvailable:
>         // New audio route (e.g., AirPods connected) — audio already routing correctly
>         break
>
>     default:
>         break
>     }
> }
> ```

### Background audio

> [accordion] Keep voice sessions alive in the background
>
> By default, iOS suspends apps after they move to the background. To keep a voice session
> running while the user is on another screen:
>
> 1. In Xcode, open your target's **Signing & Capabilities** tab.
> 2. Click **+ Capability** and add **Background Modes**.
> 3. Check **Audio, AirPlay, and Picture in Picture**.
>
> This adds the following to your `Info.plist`:
>
> ```xml theme={null}
> <key>UIBackgroundModes</key>
> <array>
>     <string>audio</string>
> </array>
> ```
>
> With this capability, the `AVAudioSession` remains active while the app is in the background
> and the voice session continues uninterrupted.
>
> > [!WARNING]
> >
> > Apple's App Store review guidelines require that background audio has a genuine user benefit.
> > Voice assistant apps qualify — background music players in disguise do not.

***

> [!WARNING]
>
> Never hard-code your credential. Read it from an environment variable:

```python Python theme={null}
import os
from humain_kiosk import KioskClient

client = KioskClient(credential=os.environ["HUMAIN_CREDENTIAL"])
```

```go Go theme={null}
import (
    "os"
    kiosk "github.com/humain/kiosk-go"
)

client := kiosk.New(kiosk.Config{
    Credential: os.Getenv("HUMAIN_CREDENTIAL"),
})
```

```csharp .NET theme={null}
// appsettings.json: { "Humain": { "Credential": "hk_live_..." } }
var client = new KioskClient(new KioskClientOptions
{
    Credential = configuration["Humain:Credential"],
});
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.