Implement Chromecast on iOS using Swift

I am going to implement Google’s Chromecast on iOS using Swift.
I will be using Google Chromecast 2 device and Xcode 9.4.1.

First let’s read the Google Cast documentation to get familiar with it’s implementation. After getting familiar with the Get Started section, we need to register the Google Chromecast device. There will be a one time $5 fee for Google Cast Developer Registration. After paying, we will be redirected to Google Cast SDK Developer Console

Setup for development

Assuming you already have installed the Google Home app or the Chrome browser extension, if not please do. After that we will register the Chromecast device. Click on Add New Application. For now I will choose Custom Receiver and fill up the necessary informations. 

Next, under the Cast Receiver Devices click on Add New Device and enter your Chromecast’s Serial Number and your own description. After saving the device, it should indicate in the status that it is Registering. Take note that it will take several minutes to register the device.

Sender Application

Now we will create the sender application for Chromecast. Assuming you already have setup your Xcode project using Cocoapods, if not please do. Sender app refers to our mobile device or laptop which will handle the playback.

Open your Podfile and add the following line below and then type pod install on your terminal.
pod ‘google-cast-sdk’, ‘4.3.1’

Integrate CAF (Cast Application Framework)

Open AppDelegate.swift and inside the method didFinishLaunchingWithOptions add the following lines below

import UIKit
import GoogleCast
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    private let appId = "0FFF55BD"
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // Initialize Google Cast SDK
        let discoveryCriteria = GCKDiscoveryCriteria(applicationID: appId)
        let castOptions = GCKCastOptions(discoveryCriteria: discoveryCriteria)
        GCKCastContext.setSharedInstanceWith(castOptions)
        GCKLogger.sharedInstance().delegate = self
        return true
    }
extension AppDelegate: GCKLoggerDelegate {
    func logMessage(_ message: String, at level: GCKLoggerLevel, fromFunction function: String, location: String) {
        print("Message from Chromecast = (message)")
    }
}

Take note of the appId variable, that is the Application ID when you registered your application in the Google Cast SDK Developer Console.

Let’s add a Cast button in our navigation bar. Open ViewController.swiftand add the following code block.

import UIKit
import GoogleCast
class HomeViewController: UIViewController {
    @IBOutlet weak var navItem: UINavigationItem!
    override func viewDidLoad() {
        super.viewDidLoad()
        let castButton = GCKUICastButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
        castButton.tintColor = UIColor.red
        let castBarButtonItem = UIBarButtonItem(customView: castButton)
        navigationItem.rightBarButtonItem = castBarButtonItem
    }
}

Build and run. It should have a red Chromecast button appear on the right side of the navigation bar.


Tapping on the Cast button should display a ViewController that lists the Chromecast devices that were found.


If at first the Cast button will not display on the navigation bar, try to turn on your Chromecast device. Based on my testing, the Cast button will not appear if there are no Chromecast devices found.

I used the value https://lawgimenez.me as the value for Receiver Application URL. When everything is successful, the Chromecast should be able to cast my website to my TV. When I tap on Gimenez’s Room TV, it should display the contents of my URL. I both tested this implementation on both real device and emulator.

IMG_0714

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

  You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

  You are commenting using your Google account. Log Out /  Change )

Twitter picture

  You are commenting using your Twitter account. Log Out /  Change )

  You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close