# client-sdk-swift **Repository Path**: lichongbing/client-sdk-swift ## Basic Information - **Project Name**: client-sdk-swift - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-23 - **Last Updated**: 2025-09-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README The LiveKit icon, the name of the repository and some sample code in the background. # iOS/macOS Swift SDK for LiveKit Use this SDK to add realtime video, audio and data features to your Swift app. By connecting to LiveKit Cloud or a self-hosted server, you can quickly build applications such as multi-modal AI, live streaming, or video calls with just a few lines of code. [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flivekit%2Fclient-sdk-swift%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/livekit/client-sdk-swift) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flivekit%2Fclient-sdk-swift%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/livekit/client-sdk-swift) ## Docs & Example app > [!NOTE] > Version 2 of the Swift SDK contains breaking changes from Version 1. > Read the [migration guide](https://docs.livekit.io/guides/migrate-from-v1/) for a detailed overview of what has changed. Docs and guides are at [https://docs.livekit.io](https://docs.livekit.io). There is full source code of a [iOS/macOS Swift UI Example App](https://github.com/livekit/client-example-swift). For minimal examples view this repo 馃憠 [Swift SDK Examples](https://github.com/livekit/client-example-collection-swift) ## Installation LiveKit for Swift is available as a Swift Package. ### Package.swift Add the dependency and also to your target ```swift title="Package.swift" let package = Package( ... dependencies: [ .package(name: "LiveKit", url: "https://github.com/livekit/client-sdk-swift.git", .upToNextMajor("2.7.2")), ], targets: [ .target( name: "MyApp", dependencies: ["LiveKit"] ) ] } ``` ### XCode Go to Project Settings -> Swift Packages. Add a new package and enter: `https://github.com/livekit/client-sdk-swift` ### CocoaPods For installation using CocoaPods, please refer to this [guide](./Docs/cocoapods.md). ## iOS Usage LiveKit provides an UIKit based `VideoView` class that renders video tracks. Subscribed audio tracks are automatically played. ```swift import LiveKit import UIKit class RoomViewController: UIViewController { lazy var room = Room(delegate: self) lazy var remoteVideoView: VideoView = { let videoView = VideoView() view.addSubview(videoView) // Additional initialization ... return videoView }() lazy var localVideoView: VideoView = { let videoView = VideoView() view.addSubview(videoView) // Additional initialization ... return videoView }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white let url = "ws://your_host" let token = "your_jwt_token" Task { do { try await room.connect(url: url, token: token) // Connection successful... // Publishing camera & mic... try await room.localParticipant.setCamera(enabled: true) try await room.localParticipant.setMicrophone(enabled: true) } catch { // Failed to connect } } } } extension RoomViewController: RoomDelegate { func room(_: Room, participant _: LocalParticipant, didPublishTrack publication: LocalTrackPublication) { guard let track = publication.track as? VideoTrack else { return } DispatchQueue.main.async { self.localVideoView.track = track } } func room(_: Room, participant _: RemoteParticipant, didSubscribeTrack publication: RemoteTrackPublication) { guard let track = publication.track as? VideoTrack else { return } DispatchQueue.main.async { self.remoteVideoView.track = track } } } ``` ### Screen Sharing See [iOS Screen Sharing instructions](Docs/ios-screen-sharing.md). ## Integration Notes ### Submitting to the App Store, DSYMs `LiveKitWebRTC.xcframework` binary framework, which is the main dependency of the SDK, does not contain DSYMs. Submitting the app to the App Store will result in a following warning: ``` The archive did not include a dSYM for the LiveKitWebRTC.framework with the UUIDs [...]. Ensure that the archive's dSYM folder includes a DWARF file for LiveKitWebRTC.framework with the expected UUIDs. ``` It will **not prevent** the app from being submitted to the App Store or passing the review process. If you are building a customized version of the [LiveKitWebRTC](https://github.com/webrtc-sdk/webrtc), you can use the [build script](https://github.com/webrtc-sdk/webrtc-build/blob/main/build/apple/xcframework.sh) in `DEBUG` mode to generate them locally. ### Thread safety Since `VideoView` is a UI component, all operations (read/write properties etc) must be performed from the `main` thread. Other core classes can be accessed from any thread. Delegates will be called on the SDK's internal thread. Make sure any access to your app's UI elements are from the main thread, for example by using `@MainActor` or `DispatchQueue.main.async`. ### Swift 6 LiveKit is currently compiled using Swift 6.0 with full support for strict concurrency. Apps compiled in Swift 6 language mode will not need to use `@preconcurrency` or `@unchecked Sendable` to access LiveKit classes. ### Memory management It is recommended to use **weak var** when storing references to objects created and managed by the SDK, such as `Participant`, `TrackPublication` etc. These objects are invalid when the `Room` disconnects, and will be released by the SDK. Holding strong reference to these objects will prevent releasing `Room` and other internal objects. `VideoView.track` property does not hold strong reference, so it's not required to set it to `nil`. ### AudioSession management LiveKit will automatically manage the underlying `AVAudioSession` while connected. The session will be set to `playback` category by default. When a local stream is published, it'll be switched to `playAndRecord`. In general, it'll pick sane defaults and do the right thing. However, if you'd like to customize this behavior, you would override `AudioManager.customConfigureAudioSessionFunc` to manage the underlying session on your own. See [example here](https://github.com/livekit/client-sdk-swift/blob/1f5959f787805a4b364f228ccfb413c1c4944748/Sources/LiveKit/Track/AudioManager.swift#L153) for the default behavior. For more audio related information see the [Audio guide](./Docs/audio.md). ### Integration with CallKit To integrate with CallKit for background-triggered incoming calls, LiveKit's audio session must be synchronized with CallKit's audio session: 1. Add `import LiveKitWebRTC` to your CallProvider file. 2. In your `CXProviderDelegate` implementation, add the following: ```swift func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession){ LKRTCAudioSession.sharedInstance().audioSessionDidActivate(audioSession) // ... } func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) { LKRTCAudioSession.sharedInstance().audioSessionDidDeactivate(audioSession) // ... } ``` You would also want to turn off automatic `AVAudioSession` configuration. ```swift AudioManager.shared.audioSession.isAutomaticConfigurationEnabled = false ``` For more audio related information see the [Audio guide](./Docs/audio.md). ### iOS Simulator limitations - Publishing the camera track is not supported by iOS Simulator. ### ScrollView performance It is recommended to turn off rendering of `VideoView`s that scroll off the screen and isn't visible by setting `false` to `isEnabled` property and `true` when it will re-appear to save CPU resources. `UICollectionViewDelegate`'s `willDisplay` / `didEndDisplaying` has been reported to be unreliable for this purpose. Specifically, in some iOS versions `didEndDisplaying` could get invoked even when the cell is visible. The following is an alternative method to using `willDisplay` / `didEndDisplaying` : ```swift // 1. define a weak-reference set for all cells private var allCells = NSHashTable.weakObjects() ``` ```swift // in UICollectionViewDataSource... public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ParticipantCell.reuseIdentifier, for: indexPath) if let cell = cell as? ParticipantCell { // 2. keep weak reference to the cell allCells.add(cell) // configure cell etc... } return cell } ``` ```swift // 3. define a func to re-compute and update isEnabled property for cells that visibility changed func reComputeVideoViewEnabled() { let visibleCells = collectionView.visibleCells.compactMap { $0 as? ParticipantCell } let offScreenCells = allCells.allObjects.filter { !visibleCells.contains($0) } for cell in visibleCells.filter({ !$0.videoView.isEnabled }) { print("enabling cell#\(cell.hashValue)") cell.videoView.isEnabled = true } for cell in offScreenCells.filter({ $0.videoView.isEnabled }) { print("disabling cell#\(cell.hashValue)") cell.videoView.isEnabled = false } } ``` ```swift // 4. set a timer to invoke the func self.timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { [weak self] _ in self?.reComputeVideoViewEnabled() }) // alternatively, you can call `reComputeVideoViewEnabled` whenever cell visibility changes (such as scrollViewDidScroll(_:)), // but this will be harder to track all cases such as cell reload etc. ``` For the full example, see 馃憠 [UIKit Minimal Example](https://github.com/livekit/client-example-collection-swift/tree/main/uikit-minimal) # Frequently asked questions ### How to publish camera in 60 FPS ? - Create a `LocalVideoTrack` by calling `LocalVideoTrack.createCameraTrack(options: CameraCaptureOptions(fps: 60))`. - Publish with `LocalParticipant.publish(videoTrack: track, publishOptions: VideoPublishOptions(encoding: VideoEncoding(maxFps: 60)))`. # Known issues ### Avoid crashes on macOS Catalina If your app is targeting macOS Catalina, make sure to do the following to avoid crash (ReplayKit not found): 1. Explicitly add "ReplayKit.framework" to the Build Phases > Link Binary with Libraries section 2. Set it to Optional replykit - I am not sure why this is required for ReplayKit at the moment. - If you are targeting macOS 11.0+, this is not required. # Getting help / Contributing Please join us on [Slack](https://livekit.io/join-slack) to get help from our devs / community members. We welcome your contributions(PRs) and details can be discussed there.
LiveKit Ecosystem
LiveKit SDKsBrowseriOS/macOS/visionOSAndroidFlutterReact NativeRustNode.jsPythonUnityUnity (WebGL)ESP32
Server APIsNode.jsGolangRubyJava/KotlinPythonRustPHP (community).NET (community)
UI ComponentsReactAndroid ComposeSwiftUIFlutter
Agents FrameworksPythonNode.jsPlayground
ServicesLiveKit serverEgressIngressSIP
ResourcesDocsExample appsCloudSelf-hostingCLI