Swift Components Quickstart

Get started with LiveKit and Swift Components

1. Install LiveKit SDK

Go to Project Settings > Package Dependencies. Add a new package and enter URL:

https://github.com/livekit/components-swift

See Adding package dependencies to your app for more details.

2. Declare permissions

Camera and microphone usage need to be declared in your Info.plist file.

<dict>
...
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) uses your camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) uses your microphone</string>
...
</dict>

Your application can still run a voice call when it is switched to the background if the background mode is enabled. Select the app target in Xcode, click the Capabilities tab, enable Background Modes, and check Audio, AirPlay, and Picture in Picture.

Your Info.plist should have the following entries:

<dict>
...
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>

3. Join a Room

Note that this example hardcodes a token we generated for you that expires in 2 hours. In a real app, you’ll need your server to generate a token for you.

// !! Note !!
// This sample hardcodes a token which expires in 2 hours.
let wsURL = "<your LiveKit server URL>"
let token = "<generate a token>"
// In production you should generate tokens on your server, and your client
// should request a token from your server.
import LiveKit
import LiveKitComponents
import SwiftUI
@main
struct ComponentsExampleApp: App {
var body: some Scene {
WindowGroup {
RoomScope(url: wsURL, token: token, connect: true, enableCamera: true) {
LazyVStack {
ForEachParticipant {
VStack {
ForEachTrack(filter: .video) { trackReference in
VideoTrackView(trackReference: trackReference)
.frame(width: 500, height: 500)
}
}
}
}
}
}
}
}

For more details, you can reference the components example app.

4. Create a backend server to generate tokens

Set up a server to generate tokens for your app at runtime by following this guide: Generating Tokens.