Getting started

This guide walks you through adding PRESTOplay for Apple to your project, initializing the SDK, and playing your first stream.

Prerequisites

Add the SDK to your project

The SDK is available via Swift Package Manager (SPM), CocoaPods, or manual XCFramework integration.

Swift Package Manager

Add the PRESTOplay package repository to your Xcode project:

  1. In Xcode, select File > Add Package Dependencies

  2. Enter the Castlabs SPM repository URL

  3. Select the packages you need (at minimum PRESTOplay and CastlabsApple for HLS playback)

CocoaPods

Add the Castlabs pod source and dependencies to your Podfile:

source 'https://github.com/castlabs/client_podspecs.git'

use_frameworks!

pod 'PRESTOplay', '<version>'
pod 'CastlabsApple', '<version>'

Then run pod install.

Manual integration

  1. Unzip the SDK bundle

  2. Add PRESTOplay.xcframework to your project under Frameworks, Libraries, and Embedded Content with Embed & Sign

  3. Repeat for any additional plugin frameworks (e.g., CastlabsApple.xcframework)

Initialize the SDK

Call PRESTOplaySDK.shared.setup() once during app startup, passing your license key and the plugins you want to activate. For HLS playback, you need at minimum the HLSPlugin:

import PRESTOplay
import CastlabsApple

let error = PRESTOplaySDK.shared.setup("your-license-key", [HLSPlugin()])
if let error = error {
    print("SDK initialization failed: \(error)")
}

You’re also responsible for configuring the audio session. If your app only does playback, AVAudioSessionCategory.playback is sufficient:

import AVFoundation

let audioSession = AVAudioSession.sharedInstance()
do {
    try audioSession.setCategory(.playback)
    try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
} catch {
    print("Cannot initialize audio session")
}

Set up error handling

Player operations happen asynchronously, so errors are reported through a global callback. Attach your handler before opening any content:

PRESTOplaySDK.shared.onError = { component, error in
    print("Error from \(component): \(error)")
}

Start playback

Create a player, configure it with a stream URL, attach it to a view layer, and open the content:

import PRESTOplay
import CastlabsApple

// Create the player
var player = PRESTOplaySDK.shared.player()

guard let contentURL = URL(string: "https://demo.cf.castlabs.com/media/prestohls/master.m3u8")
else { return }

// Configure and load the player
let config = PlayerConfiguration(with: contentURL)
player.load(config: config)

// Handle state changes
player.onState = { previous, state in
    switch state {
    case .ready:
        print("Player ready")
    case .playing:
        print("Playing")
    default:
        break
    }

    if let error = state.playerError {
        print("Error: \(error)")
    }
}

// Attach the player to a view layer
player.attach(to: view.layer)

// Start playback
player.open(autoplay: true)

License user identification

If your license requires a user ID, set it on the PlayerConfiguration before loading:

let config = PlayerConfiguration(with: contentURL)
config.userId = "your-user-id"
player.load(config: config)
Next topic: Thumbnails
Previous topic: PRESTOplay for Apple