Talking to Chromecast from iOS

Part One: Easily find Chromecast devices on your local network

Now that Google has opened up the Chromecast API for anyone to play with, it’s possibile to create iOS applications that can leverage the $35 device as a way to display to HDMI devices wirelessly. In this series of tutorials, we’ll go over the API, starting with configuring your project to use the framework, and finding devices out on your local network to play with.

Let’s assume you’ve set up a Chromecast device attached to an HDMI TV and have it configured for your local network. Now it’s time to get an App set up to use it. We’ll use the iPhone Simulator in these examples, since it can talk to Chromecast devices just like a physical device, as long as the Mac you are developing on is on the same LAN as the Chromecast dongle.

Begin by creating a project, as usual. For this example, I used a single-view Storyboarded app. I set up an UITableView inside the default UIView, hooking it’s datasource and delegate to the default view controller the wizard had created. Next, I went to the Google Google Cast API page and downloaded the iOS framework, then used the “Add Files…” project option to add the framework to the project, copying in the files.

After that, your project should look something like this:

Chromecast iOS Project

The important thing to notice is the presence of the GoogleCast framework in the project. Now we need to make two changes to the project build settings. Firstly, you need to set the Architectures flag to the non-64 bit version, because the framework isn’t compiled for 64 bit architectures yet. Secondly, you need to add “-ObjC” to the Other Linker Flags parameter, so that you don’t end up with unresolved library values at runtime from the framework.

The last step for this week is to implement the delegate and datasource for the UITableView, so that we can actually get a look at the available devices. Here’s the code I came up with:

The key calls here are the GCKDeviceScanner -startScan and the two delegate methods deviceDidComeOnline and deviceDidGoOffline. One you call startScan, any object that has registered as a listener will be notified whenever a new device is discovered or an old one disappears. If you run the application on a network with a device installed on it, you should see something like this:

Screen Shot of Running Chromecase Device FinderNow that we have confirmed that our iOS device can see the local Chromecast dongle, we’ll look next week into pushing some simple content to it.

 

tags: , , , ,

Get the O’Reilly Programming Newsletter

Weekly insight from industry insiders. Plus exclusive content and offers.