Swift SDK

Build native video experiences for iOS and macOS with Swift

Installation

Choose your preferred package manager

Add to your Package.swift or via Xcode:

Package.swift
1dependencies: [
2    .package(
3        url: "https://github.com/baxcloud/swift-sdk", 
4        from: "1.0.0"
5    )
6]
Or via Xcode:
  1. File → Add Packages...
  2. Enter package URL: https://github.com/baxcloud/swift-sdk
  3. Select version and add to your target
All required dependencies are bundled with the SDK

iOS Configuration

Required permissions in Info.plist

Info.plist
1<key>NSCameraUsageDescription</key>
2<string>We need access to your camera for video calls</string>
3<key>NSMicrophoneUsageDescription</key>
4<string>We need access to your microphone for audio calls</string>

Background Audio (Optional)

For background audio support, add:

Info.plist
1<key>UIBackgroundModes</key>
2<array>
3    <string>audio</string>
4</array>

Quick Start

Get started with Swift SDK

1. Initialize Client

1import BaxCloudClient
2
3let client = BaxCloudClient(config: BaxCloudConfig(
4    projectId: "your-project-id",
5    apiKey: "your-api-key"
6))

2. Connect to a Room

1Task {
2    do {
3        let room = try await client.connect(
4            options: BaxcloudRoomOptions(
5                roomName: "my-room",
6                participant: BaxcloudUser(
7                    userId: "user-123",
8                    name: "John Doe",
9                    isHost: true
10                ),
11                liveType: .videoCall
12            )
13        )
14        
15        print("Connected to room: \(room.name)")
16    } catch {
17        print("Connection failed: \(error)")
18    }
19}

3. Render Video

1import SwiftUI
2import BaxCloudClient
3
4struct VideoGridView: View {
5    let room: Room
6    
7    var body: some View {
8        let participants = Array(room.participants.values)
9        
10        LazyVGrid(columns: [
11            GridItem(.flexible()),
12            GridItem(.flexible())
13        ], spacing: 8) {
14            ForEach(participants, id: \.identity) { participant in
15                VideoParticipantView(
16                    participant: participant,
17                    isLocal: participant.identity == room.localParticipant?.identity
18                )
19            }
20        }
21        .padding()
22    }
23}
24
25struct VideoParticipantView: View {
26    let participant: Participant
27    let isLocal: Bool
28    
29    var body: some View {
30        ZStack {
31            // Video view
32            if let videoTrack = participant.videoTrack {
33                VideoView(track: videoTrack)
34                    .aspectRatio(3/4, contentMode: .fill)
35                    .clipped()
36            } else {
37                Color.gray.opacity(0.8)
38                Image(systemName: "person.fill")
39                    .font(.system(size: 48))
40                    .foregroundColor(.white.opacity(0.5))
41            }
42            
43            // Participant name overlay
44            VStack {
45                Spacer()
46                HStack {
47                    if participant.isMuted {
48                        Image(systemName: "mic.slash.fill")
49                            .font(.system(size: 12))
50                            .foregroundColor(.red)
51                    }
52                    
53                    Text("\(participant.name)\(isLocal ? " (You)" : "")")
54                        .font(.system(size: 12, weight: .medium))
55                        .foregroundColor(.white)
56                    
57                    Spacer()
58                }
59                .padding(8)
60                .background(Color.black.opacity(0.5))
61            }
62        }
63        .cornerRadius(8)
64    }
65}

4. Disconnect

1await client.disconnect()

Usage Examples

SwiftUI and UIKit examples

ContentView.swift
1import SwiftUI
2import BaxCloudClient
3
4struct VideoCallView: View {
5    @StateObject private var client = BaxCloudClient(
6        config: BaxCloudConfig(
7            projectId: "your-project-id",
8            apiKey: "your-api-key"
9        )
10    )
11    @State private var room: Room?
12    @State private var error: Error?
13    
14    var body: some View {
15        VStack {
16            if let error = error {
17                Text("Error: \(error.localizedDescription)")
18            } else if room == nil {
19                Text("Connecting...")
20                    .onAppear {
21                        connect()
22                    }
23            } else {
24                Text("Connected to room: \(room?.name ?? "")")
25            }
26        }
27    }
28    
29    private func connect() {
30        Task {
31            do {
32                let connectedRoom = try await client.connect(
33                    options: BaxcloudRoomOptions(
34                        roomName: "video-call-123",
35                        participant: BaxcloudUser(
36                            userId: "user-1",
37                            name: "Alice",
38                            isHost: true
39                        ),
40                        liveType: .videoCall
41                    )
42                )
43                
44                await MainActor.run {
45                    self.room = connectedRoom
46                }
47            } catch {
48                await MainActor.run {
49                    self.error = error
50                }
51            }
52        }
53    }
54}

API Reference

Core classes and methods

BaxCloudClient

The main client class with async/await support.

1// Connect
2let room = try await client.connect(options: options)
3
4// Enable camera/microphone
5try await client.enableCamera()
6try await client.enableMicrophone()
7
8// Disconnect
9await client.disconnect()
10
11// Observe connection state (Combine)
12client.$connectionState
13    .sink { state in
14        print("State: \(state)")
15    }

BaxCloudRoomController

Singleton controller for advanced room features.

1let controller = BaxCloudRoomController.shared
2controller.initialize(client: client)
3
4// Host controls
5try await controller.muteAll()
6try await controller.kickParticipant(userId: "user-2")
7
8// Chat
9try await controller.sendChatMessage("Hello!")
10
11// Event handlers
12controller.onChatMessage { message in
13    print("\(message.userName): \(message.message)")
14}

Recording

1// Start recording
2let recording = try await client.startRecording(
3    options: BaxcloudStartRecordingOptions(
4        roomName: "my-room",
5        fileType: "MP4"
6    )
7)
8
9// Stop recording
10try await client.stopRecording(egressId: recording.egressId)
11
12// List recordings
13let recordings = try await client.listRecordings(
14    options: BaxcloudListRecordingsOptions(
15        status: "COMPLETED",
16        page: 1,
17        limit: 10
18    )
19)

Best Practices

Recommendations for Swift development

Use async/await

All SDK methods are async. Use Task for asynchronous operations

Request Permissions

Use AVCaptureDevice.requestAccess before connecting

Handle App Lifecycle

Disconnect in viewWillDisappear or deinit

Use Combine for State

Observe connectionState using Combine or SwiftUI's @Published