Search code examples
ioschromecast

Chromecast Channels in multiple classes


I have a GCKCastChannel to send and receive messages in my app from the Chromecast receiver file.

My biggest issue that my app has multiple classes for all the different views, I can send and receive messages on the view that casts the app to the Chromecast, although when I change views I can't send messages.

I used

if (!self.deviceManager || !self.deviceManager.isConnected) { bla bla bla } else {bla bla bla}

to check if the deviceManager knew if we were connected etc. Although it came back saying that we weren't connected to the Chromecast.

Any ideas on how I can send messages from other classes would be much appreciated!

Mark


Solution

  • The GCKDeviceManager manages a connection, so if you create a new one it isn't aware of the existing session. You've got a couple of options, that boil down to sharing the connection object:

    1. Put your DeviceManager (etc.) on the AppDelegate in order to share them between your different controllers. This is broadly the approach the CastVideos sample uses: https://github.com/googlecast/CastVideos-ios/blob/master/Classes/AppDelegate.h#L24
    2. Create a custom class as a singleton to contain the DeviceManager, and refer to that for any other media. You can follow the [class sharedInstance] convention then to access it in any controller.

    There's some good discussion on sharing objects between classes in several other SO questions - for example How do I share an object between UIViewControllers on iPhone?