1. Install LiveKit SDK
let package = Package(...dependencies: [.package(name: "LiveKit", url: "<https://github.com/livekit/client-sdk-swift.git>", .upToNextMajor("1.0.0")),],targets: [.target(name: "MyApp",dependencies: ["LiveKit"])])
Go to Project Settings -> Swift Packages.
Add a new package and enter: https://github.com/livekit/client-sdk-swift
2. Join a Room with video
Note that this example hardcodes a token we generated for you that expires in 15 minutes. In a real app, you’ll need your server to generate a token for you.
import LiveKitimport UIKit// !! Note !!// This sample hardcodes a token which expires in 15 minutes.private let url: String = "<wsURL>"private let token: String = "<token>";// In production you should generate tokens on your server, and your client// should request a token from your server.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 = .whiteroom.connect(url, token).then { room in// Publish camera & microom.localParticipant?.setCamera(enabled: true)room.localParticipant?.setMicrophone(enabled: true)}.catch { error in// failed to connect}}}extension RoomViewController: RoomDelegate {func room(_ room: Room, localParticipant: LocalParticipant, didPublish publication: LocalTrackPublication) {guard let track = publication?.track as? VideoTrack else {return}DispatchQueue.main.async {localVideoView.track = track}}func room(_ room: Room, participant: RemoteParticipant, didSubscribe publication: RemoteTrackPublication, track: Track) {guard let track = track as? VideoTrack else {return}DispatchQueue.main.async {remoteVideoView.track = track}}}
3. Create a backend server to generate tokens
Use our Server SDKs to create a backend server than generates tokens for your app at runtime: