Chromecast Integration
Add Chromecast (Google Cast) support when using BBChannelWithPlayerView.
Chromecast is provided by BBNativePlayerKit, which wraps the native Google Cast SDK.
Prerequisites
Info.plist
Add the required keys:
<key>NSLocalNetworkUsageDescription</key>
<string>This app uses the local network to discover Cast devices.</string>
<key>NSBonjourServices</key>
<array>
<string>_googlecast._tcp</string>
<string>_1F61A3A5._googlecast._tcp</string>
</array>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app uses Bluetooth for casting video to external devices.</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>This app uses Bluetooth for casting video to external devices.</string>
Replace 1F61A3A5 with your Cast application ID if using a custom receiver.
These entries are required. Without them, Cast device discovery causes excessive CPU usage and battery drain (the SDK logs warnings about missing NSBonjourServices).
No AppDelegate Setup Needed
Unlike React Native, the SwiftUI SDK handles Cast context initialization lazily through BBNativePlayerKit. No explicit GCKCastContext.setSharedInstanceWith() call is needed.
Cast Button
Use CastButtonView to render the native Google Cast button. It auto-shows when a Cast device is discovered and auto-hides when none are available.
import BBNativePlayerKit
struct ChannelScreen: View {
@StateObject private var playerVM = BBChannelWithPlayerViewModel()
var body: some View {
VStack(spacing: 0) {
// Header with cast button
HStack {
Spacer()
CastButtonView(tintColor: .white)
.frame(width: 24, height: 24)
}
.padding()
.background(Color.black)
// Channel with native player
BBChannelWithPlayerView(
channelUrl: "https://demo.bbvms.com/ch/channel_name.json",
viewModel: playerVM,
callbacks: BBChannelCallbacks()
)
}
}
}
CastButtonView
A UIViewRepresentable wrapper around the native GCKUICastButton.
| Parameter | Type | Description |
|---|---|---|
tintColor | UIColor | Icon color. Default: .white |
How It Works
BBChannelWithPlayerView handles cast routing automatically via castMode: "auto":
- Connect first, then play — video routes directly to the Cast device
- Play first, then connect — playback transfers to Cast at the current position
- Switch clips while casting — new content replaces the current Cast stream
- Disconnect — playback resumes locally on next play
No additional configuration or dialogs are needed.
Tips
- Place
CastButtonViewin the app header so it's always accessible - The cast button is invisible when no devices are available — use fixed frame dimensions so your layout doesn't shift
- Cast device discovery requires the device to be on the same Wi-Fi network as the Chromecast