# GCKCastChannel Class

[Instance Methods](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#pub-methods) \| [Properties](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#properties) GCKCastChannel Class Reference

## Overview

A virtual communication channel for exchanging messages between a Cast sender and a Cast receiver.

Each channel is tagged with a unique namespace, so multiple channels may be multiplexed over a single network connection between a sender and a receiver.

A channel must be registered with a [GCKCastSession](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_session "A class that manages a Cast session with a receiver device. ") before it can be used. When the associated session is established, the channel will be connected automatically and can then send and receive messages.

Subclasses should implement the [didReceiveTextMessage: (GCKCastChannel)](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#ad8d937fc0cfd104f02d7d2951d925cdd) method to process incoming messages, and will typically provide additional methods for sending messages that are specific to a given namespace.

Inherits NSObject.

Inherited by [GCKGenericChannel](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_generic_channel).

|---|---|
| ## Instance Method Summary ||
| (instancetype) | - [initWithNamespace:](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a95b4ed119093a3de422528e8bee9b14e) |
|   | Designated initializer. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a95b4ed119093a3de422528e8bee9b14e) |
|   ||
| (instancetype) | - [init](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a5c30573fc91241959e4ffe894b539d42) |
|   | Default initializer is not available. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a5c30573fc91241959e4ffe894b539d42) |
|   ||
| (void) | - [didReceiveTextMessage:](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#ad8d937fc0cfd104f02d7d2951d925cdd) |
|   | Called when a text message has been received on this channel. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#ad8d937fc0cfd104f02d7d2951d925cdd) |
|   ||
| (BOOL) | - [sendTextMessage:error:](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#ab7f595487ce145f38b2e82e55126789b) |
|   | Sends a text message on this channel. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#ab7f595487ce145f38b2e82e55126789b) |
|   ||
| (NSInteger) | - [generateRequestID](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a8c83fab95cb5d463d28a0426dd4d3e17) |
|   | Generates a request ID for a new message. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a8c83fab95cb5d463d28a0426dd4d3e17) |
|   ||
| (nullable NSNumber \*) | - [generateRequestNumber](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a204ff88c0c58847a89d59e9f30f85bb6) |
|   | A convenience method which wraps the result of [generateRequestID](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a8c83fab95cb5d463d28a0426dd4d3e17) in an [**NSNumber**](https://goo.gl/gY6NGU). [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a204ff88c0c58847a89d59e9f30f85bb6) |
|   ||
| (void) | - [didConnect](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a82a79e3e3f45509df5d0b6b4296a8529) |
|   | Called when this channel has been connected, indicating that messages can now be exchanged with the Cast device over this channel. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a82a79e3e3f45509df5d0b6b4296a8529) |
|   ||
| (void) | - [didDisconnect](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a83f1f5750468a8442116610ff3a6a6a9) |
|   | Called when this channel has been disconnected, indicating that messages can no longer be exchanged with the Cast device over this channel. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a83f1f5750468a8442116610ff3a6a6a9) |
|   ||
| (void) | - [didChangeWritableState:](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a35de09db2c61864fb524839569266632) |
|   | Called when the writable state of this channel has changed. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a35de09db2c61864fb524839569266632) |
|   ||

|---|---|
| ## Property Summary ||
| NSString \* | [protocolNamespace](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#abf91f5de1dfcc467c94d9f711f219625) |
|   | The channel's namespace. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#abf91f5de1dfcc467c94d9f711f219625) |
|   ||
| BOOL | [isConnected](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#aef5eb750a266e9549fd81f45d63b36eb) |
|   | A flag indicating whether this channel is currently connected. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#aef5eb750a266e9549fd81f45d63b36eb) |
|   ||
| BOOL | [isWritable](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a922912c939a4ebb1266d00f550830537) |
|   | A flag indicating whether this channel is currently writable. [More...](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a922912c939a4ebb1266d00f550830537) |
|   ||

## Method Detail

|---|---|---|---|---|
| - (instancetype) initWithNamespace: |   | (NSString \*) | *protocolNamespace* |   |

Designated initializer.

Constructs a new [GCKCastChannel](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel "A virtual communication channel for exchanging messages between a Cast sender and a Cast receiver...") with the given namespace.

Parameters
:

    |---|---|
    | protocolNamespace | The namespace. |


Implemented in [GCKGenericChannel](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_generic_channel#ac355b4afa1aa5011122abb8340dea7a0).

|---|---|---|---|
| - (instancetype) init |   |   |   |

Default initializer is not available.

|---|---|---|---|---|
| - (void) didReceiveTextMessage: |   | (NSString \*) | *message* |   |

Called when a text message has been received on this channel.

The default implementation is a no-op.

Parameters
:

    |---|---|
    | message | The message. |


|---|---|---|---|
| - (BOOL) sendTextMessage: |   | (NSString \*) | *message* |
| error: |   | ([GCKError](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_error) \*_Nullable \*_Nullable) | *error* |
|   |   |   |   |

Sends a text message on this channel.

Parameters
:

    |---|---|
    | message | The message. |
    | error | A pointer at which to store the error result. May be `nil`. |


Returns
:   `YES` on success or `NO` if the message could not be sent.

|---|---|---|---|
| - (NSInteger) generateRequestID |   |   |   |

Generates a request ID for a new message.

Returns
:   The generated ID, or [kGCKInvalidRequestID](https://developers.google.com/cast/docs/reference/ios/g_c_k_common_8h#aad76567f243bc1b4fb113abd97bf3327) if the channel is not currently connected.

|---|---|---|---|
| - (nullable NSNumber \*) generateRequestNumber |   |   |   |

A convenience method which wraps the result of [generateRequestID](https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel#a8c83fab95cb5d463d28a0426dd4d3e17) in an [**NSNumber**](https://goo.gl/gY6NGU).

Returns
:   The generated ID, or `nil` if the channel is not currently connected.

|---|---|---|---|
| - (void) didConnect |   |   |   |

Called when this channel has been connected, indicating that messages can now be exchanged with the Cast device over this channel.

The default implementation is a no-op.

|---|---|---|---|
| - (void) didDisconnect |   |   |   |

Called when this channel has been disconnected, indicating that messages can no longer be exchanged with the Cast device over this channel.

The default implementation is a no-op.

|---|---|---|---|---|
| - (void) didChangeWritableState: |   | (BOOL) | *isWritable* |   |

Called when the writable state of this channel has changed.

The default implementation is a no-op.

Parameters
:

    |---|---|
    | isWritable | Whether the channel is now writable. |


Since
:   4.0

## Property Detail

|---|---|
| |---| | - (NSString\*) protocolNamespace | | readnonatomiccopy |

The channel's namespace.

|---|---|
| |---| | - (BOOL) isConnected | | readnonatomicassign |

A flag indicating whether this channel is currently connected.

|---|---|
| |---| | - (BOOL) isWritable | | readnonatomicassign |

A flag indicating whether this channel is currently writable.

Since
:   4.0