Google Cast — iOS Integration

Pratheesh Bennet
4 min readFeb 22, 2022

--

Hey all, today we are going to see a simple integration of Google Cast SDK into your iOS app to cast a video. Google has provided an excellent sample app. To kick start, the sample app is bit complicated. For simplicity I am going to walk through only the core blocks step by step deduced from the available sample app.

Basic Terminologies:

Sender App: The sender app in our case is the iOS app that casts the video.

Receiver App: The receiver app is web receiver application is an HTML5/JavaScript application that runs on the Web Receiver device, such as a Chromecast.

Google Cast iOS Integration

Step 1: Enable Access wifi information in your provisioning profile in you apple developer portal. Under Certificates, Identifiers & Profiles -> enable access wifi information

Step 2: In your Xcode project under target -> Signing and capabilities -> Add Access Wifi Information. Once you do this, inside your application, project.entitlements will be automatically created.

Step 3: Register your receiver app and get the App ID. Please refer the registration steps. Once you register you should be able to see the App ID. In our sample walk through we are going to use the App Id provided by Google since they already have created and registered this sample receiver app.

let kReceiverAppID = “C0868879”

Step 4: Add the following in the info.plist file

  1. Bluetooth usage description,
  2. NSLocalNetworkUsageDescription and
  3. NSBonjourServicesin the info.plist. Note that the App ID to be included in the NSBonjourServices<string>_C0868879._googlecast._tcp</string>

Step 5: Install Google-Cast SDK via cocoapods,

pod ‘google-cast-sdk’, ‘~> 4.7.0’

Step 6: Setup the context GCKCastContext in the app delegate and add a mini controller to the existing view. Copy the RootContainerViewController.swift from the sample app and paste it inside your app.

Step 7: Create the session and add the GCKSessionManagerListener observer.

Step 8: Casting the media. To cast media, your basically need to configure two classes namely, GCKMediaMetadata and GCKMediaInformationBuilder.

As the name suggests GCKMediaMetadata holds the metadata about the media being casted such as the title, description, thumbnail etc. GCKMediaInformationBuilder is used to create a request to cast the media.

Step 9: Once the load media request is initiated, the session, request states can be observed via the GCKSessionManagerListener, GCKRemoteMediaClientListener, GCKRequestDelegate delegates.

Step 10: Setup the GCKUICastButton in your app. This button is provided by the cast-SDK. This button enables to choose from the list of available devices ready for casting. Once a device is connected, the icon will be blue filled.

Example in action:

On app launch the GCKCastContext is configured in the app delegate. In the viewcontroller view did load, the session is set and the listener is added to the viewcontroller. On tapping the GCKUICastButton, the device to be casted can be selected. Give the necessary local area network access permission when prompted.

On tap of the start button, the loadMediaToCast() button is called and the media is requested with the default inbuilt controls provided by the cast-SDK.

On tapping the cast -> Choose the receiver
On tapping start the media is loaded to cast with the default controls provided by the cast SDK
Media casted

Hope this article helps you to understand the basic core blocks of the google cast sdk. Please feel to reach me at pratheesh_db@hotmail.com for any queries or help. Happy coding!!

References: https://developers.google.com/cast/docs/ios_sender/integrate

--

--

Pratheesh Bennet

Product owner Falcon Forms (www.falconforms.app). Digitize your paperwork using Falcon Forms. Build and configure your own forms in an instant.