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:

//
//  CFViewController.m
//  ChromecastFinder
//
//  Created by James Turner on 2/23/14.
//  Copyright (c) 2014 Black Bear Software, LLC. All rights reserved.
//

#import "CFViewController.h"
#import <GoogleCast/GoogleCast.h>

@interface CFViewController ()<GCKDeviceScannerListener, UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSMutableArray *devicesFound;
@property (strong, nonatomic) IBOutlet UITableView *deviceTableView;
@end

@implementation CFViewController

-(void)deviceDidComeOnline:(GCKDevice *)device {
    [self.devicesFound addObject:device];
    [self.deviceTableView reloadData];
}

-(void)deviceDidGoOffline:(GCKDevice *)device {
    [self.devicesFound removeObject:device];
    [self.deviceTableView reloadData];
}

- (void)viewDidLoad
{
    self.devicesFound = [NSMutableArray new];
    [super viewDidLoad];
    GCKDeviceScanner *scanner = [GCKDeviceScanner new];
    [scanner addListener:self];
    [scanner startScan];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark Table View Controller

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.devicesFound count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChromecastDeviceCell"];
    GCKDevice *dev = self.devicesFound[indexPath.row];
    cell.textLabel.text = dev.friendlyName;
    cell.detailTextLabel.text = dev.ipAddress;

    return cell;
}

#pragma end

@end

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: , , , ,