Chromecast from iOS using GoogleCast SDK (part 1)

sz-ashik
2 min readSep 24, 2022

--

Most Android SmartTV has Chromecast built in. Being a couch potato, you watch a cute dog/cat video on YouTube and then want to share it with your family members/roommates. Just tap into the Cast icon and BOOM it’s on TV.

And now you want to implement it into your iOS MEME generator app. After a few minutes, you understand that you have to use Google Cast SDK. So let’s dive into it.

Component diagram from https://developers.google.com/cast/docs/overview

Before writing our cast functionality, let's have a simple overview of how this SDK works. Basically, Chromecast serves as a Receiver that only shows content from a streaming media service(aka Server). So from our iOS app(Sender), we will send the information about media which will be delivered on screen. There will be a session that will help us to manage the status of the whole casting procedure.

Enough of those documented stuff. Time to implement GoogleCast.

Setup:

  • Add pod 'google-cast-sdk-no-bluetooth' into your Podfile(as we don’t want to take the Bluetooth permission).
  • Then update your pods pod update
  • Now we need to specify NSBonjourServies into Info.plist file.
<key>NSBonjourServices</key>
<array>
<string>_googlecast._tcp</string>
<string>_ABCD1234._googlecast._tcp</string>
<string>_CC1AD845._googlecast._tcp</string>
</array>
  • Also, have to add NSLocalNetworkUsageDescription into Info.plistfile
<key>NSLocalNetworkUsageDescription</key>
<string>${PRODUCT_NAME} uses the local network to discover Cast-enabled devices on your WiFi
network.</string>

N.B. To know more details about that privacy permission, I highly suggest you go through GoogleCast documentation.

Integration:

The Cast Framework has GCKCastContext a singleton object which will coordinate the Cast process. Set GCKCastContext into AppDelegate

Now add the cast button, which will show all available devices in the local network to cast.

By default, tapping this button will open the Cast dialog that is provided by the framework.

In the next part, we will see how we can cast video/image into our TV.

--

--