DEV Community

Law Gimenez
Law Gimenez

Posted on • Edited on • Originally published at law.gmnz.xyz

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)")
    }
}
Enter fullscreen mode Exit fullscreen mode

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.swift and 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
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Simulator-Screen-Shot---iPhone-8---2018-08-19-at-09.51.49
Tapping on the Cast button should display a ViewController that lists the Chromecast devices that were found.

Simulator-Screen-Shot---iPhone-8---2018-08-19-at-09.53.08
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

Originally posted at https://lwgmnz.me/2018/08/20/implement-chromecast-on-ios-using-swift/

Top comments (6)

Collapse
 
sudarvizhi3026 profile image
sudarvizhi3026

hai Law Gimenez
I have done a sample project followed your steps but for first time my project is working and the next time when iam trying to run the chromecast button is not showing

Collapse
 
lawgimenez profile image
Law Gimenez

You need to register your Chromecast device in the Google Developer Chromecast.

Collapse
 
petrosmm profile image
M Peters

Is that an absolute must??

Collapse
 
ryan6abbey profile image
ryan6abbey

Thatnks for the tutorial, but I have a question for you My controller are saying not to trust the GoocleCast class...is there any way to fix that?

Collapse
 
01234abhishek profile image
01234abhishek

In order to register in google cast framework it is free or paid?

Collapse
 
lawgimenez profile image
Law Gimenez

Paid. You need to pay $25 as far as I can remember.