Upgrading to Google Cast iOS SDK 2.4.0

· September 24, 2014

Recently I’ve been working on the samples for the Chromecast SDK on iOS. Since we’ve just updated the sample app for the brand new 2.4.0 SDK, I thought I’d briefly note down the changes I had to make, which should help if you need to update your own app from 2.3.0. As always, the full list of changes can be found in the release notes.

Update Frameworks

The first step is to drop the new GoogleCast.framework into the project. You’ll immediately notice that some classes have changed, and that there is a new dependency on SystemConfiguration.framework, so add that in your Linked Frameworks and Libraries section in the General Project Settings.

GCKDeviceScanner

To simplify the scanning and connection process, filtering has been merged right into GCKDeviceScanner. Start by removing any references to GCKDeviceFilterListener or GCKDeviceFilter as this functionality is now part of the GCKDeviceScanner directly. Remove any of the delegate methods you may have implemented in GCKDeviceFilterListener, but keep the contents - you can use the same in the GCKDeviceScannerListener methods.

Remove any references to add/removeDeviceFilterListener, then move the contents of: deviceDidComeOnline:forDeviceFilter: into the GCKDeviceScannerListener method: deviceDidComeOnline:.

For deviceDidGoOffline:forDeviceFilter:, use the GCKDeviceScannerListener equivalent: deviceDidGoOffline:.

Where you would have initiated scanning by creating a GCKDeviceFilter with criteria and a scanner, you now attach the filter criteria directly to the GCKDeviceScanner. Keep your GCKFilterCriteria object, and instead of calling initWithDeviceScanner:criteria: simply attach your criteria object to the filterCriteria property on your GCKDeviceScanner: self.deviceScanner.filterCriteria = filterCriteria;

Listing Devices

Where you would have iterated over the GCKDeviceFilter.devices array to get the currently discovered devices, you can now use the GCKDeviceScanner.devices array directly. Replace all references to the filter array with the GCKDeviceScanner devices one.

There is a gotcha here to be aware of though! The devices array is updated after the deviceDidGoOffline call is fired. If you’re using that callback to update whether or not the Cast icon appears in your app, you might hit trouble. The solution is to do your UI update in the next runloop, which you can easily do with performSelector. In the CastVideos sample, we do exactly that:

GCKDeviceManager

The other class that has received significant attention is GCKDeviceManager, with new properties and callbacks to give you more information, or easier access to that information. Some of these will require changes in your code though.

The GCKDeviceManagerDelegate method deviceManager:didReceiveStatusForApplication: has a new name. If you implemented this method, update it to the new signature: deviceManager:didReceiveApplicationMetadata:.

If you were listening for deviceManager:didDisconnectWithError to pick up network based disconnections (e.g. the WiFi disappearing), there is a small issue that that this wont fire if you disable the WiFi on your device, which you may do when testing or similar). However, you can listen to the new deviceManager:didSuspendConnectionWithReason: and will get a callback with the reason GCKConnectionSuspendReasonNetworkError (see Connection Suspension below) in any case.

Error Handling

One wider change is that many GCKDeviceManager methods now return an NSInteger containing the request ID rather than a BOOL, with immediate failure indicated by the value kInvalidRequestID, which conveniently is 0. This allows tracking whether requests subsequently fail, thanks to a new delegate protocol method: deviceManager:request:didFailWithError:.

Connection Suspension

There are new delegate methods as well for suspended and resumed connections, didSuspendConnectionWithReason and deviceManagerDidResumeConnection:rejoinedApplication. Suspended indicates the connection has been interrupted, but the device manager will attempt to connect automatically. You shouldn’t use this method to force a reconnection, but you might want to hint in the UI that the Chromecast device isn’t going to respond immediately to commands. GCKConnectionSuspendedReason is an enum indicating whether the suspension is due to the app being backgrounded, or a network related problem.

Device Status Text

The device manager protocol also has new status information callbacks. One of the most useful is a text field describing the currently running application: deviceManager:didReceiveApplicationStatusText:. This will contain “YouTube TV” or similar to indicate the app currently running. This can be helpful to display to users in case there are similarly named Cast devices on their network. The same information is conveniently available on a GCKDevice directly as the statusText property.

Launching An App

Finally, if you were previously using launchApplication:relaunchIfRunning: then its worth noting that method has been deprecated in favour of a more extensible launchApplication:withLaunchOptions. The second parameter is a new GCKLaunchOptions object. To recreate the old call, you can use: