One Logo Pool Ball
GroupActivities · IOS API REFERENCE

GroupSessionMessenger

class · iOS 15.0+ · SDK 27.1

final class GroupSessionMessenger

An object that transfers app-specific data between the devices joined in a group session. Use a `GroupSessionMessenger` object to coordinate your app's behavior across the devices attached to a group session. This object leverages the existing FaceTime communication channel to send app-specific data related to a SharePlay experience. For example, a movie-watching app might share user comments or tags while the movie plays. You create a `GroupSessionMessenger` object directly and use it to send and receive app data. Create the messenger using an active ``GroupSession`` object, which manages the underlying communication channel. Store a strong reference to your `GroupSessionMessenger` object for the lifetime of the session. The following example shows a custom object for managing a movie-watching experience. The object stores the GroupSession object associated with the experience and creates a `GroupSessionMessenger` for sending messages between participants. ``` class CowatchingExperience : ObservableObject { let groupSession: GroupSession<Trailer> let messenger: GroupSessionMessenger init(groupSession: GroupSession<Trailer>, item: Trailer) { self.groupSession = groupSession self.messenger = GroupSessionMessenger(session: groupSession) self.groupSession.join() // … } } ``` For more information about establishing a group session, see ``GroupSession``. ### Receive Messages from Other Devices The system delivers messages to your app asynchronously when they arrive and adds them to a message sequence. Use the ``messages(of:)-626qo`` or ``messages(of:)-jvoz`` method to retrieve the sequence you want and iterate over its results. Use a `for`-`in` loop with the `await` keyword to iterate asynchronously over the results. The following example shows how a Tic Tac Toe game might retrieve moves sent by the current opponent. After receiving each move, the code adds that move to the current participant's board. ``` let sessionMessenger = GroupSessionMessenger(session: groupSession) async { for await move in sessionMessenger.messages(of: TicTacToe.Move.self) { self.board.addMove(move) } } ```

Members (6)

get · iOS 16.0+

deliveryMode

TypeScript
get deliveryMode(): GroupSessionMessenger_DeliveryMode
Swift declaration
final let deliveryMode: GroupSessionMessenger.DeliveryMode

The ``DeliveryMode`` specified at initialization time (defaults to `reliable`).

method

send

TypeScript
sendToCompletion(value: ArrayBuffer, participants: Participants, completion: ((arg0: (NativeError) | null) => void)): void
Swift declaration
final func send(_ value: Data, to participants: Participants = .all, completion: @escaping ((any Error)?) -> Void)

Sends a standard data object to other participants in the group session. Use this method to send data using a standard data object. Your app is responsible for decoding and using the data it receives. - Parameters: - data: The data object to send. - participants: The recipients of the message. The default value of this parameter is the set of all active participants in the session. Use the ``Participants/only(_:)-swift.enum.case`` option to specify a subset of participants. - completion: The handler block to call with the results of the delivery attempt. The handler has no return value and takes a single Error parameter. The value of this parameter is `nil` if delivery succeeds; otherwise, it contains an appropriate error object.

method

send

TypeScript
sendToCompletion24296(value: number, participants: Participants, completion: ((arg0: (NativeError) | null) => void)): void
Swift declaration
final func send<Message>(_ value: Message, to participants: Participants = .all, completion: @escaping ((any Error)?) -> Void) where Message : Decodable, Message : Encodable

Sends a custom type to other participants in the group session. Use this method to send data that you package into a custom class or structure. The method encodes your custom structure's type information, and delivers the data only to message sequences of the same type. - Parameters: - message: The custom data structure you want to send. The data structure must conform to the <doc://com.apple.documentation/documentation/swift/codable> protocol. - participants: The recipients of the message. The default value of this parameter is the set of all active participants in the session. Use the ``Participants/only(_:)-swift.enum.case`` option to specify a subset of participants. - completion: The handler block to call with the results of the delivery attempt. The handler has no return value and takes a single <doc://com.apple.documentation/documentation/Swift/Error> parameter. The value of this parameter is `nil` if delivery succeeds; otherwise, it contains an appropriate error object.

method

send

TypeScript
sendTo(value: ArrayBuffer, participants?: Participants): Promise<void>
Swift declaration
final func send(_ value: Data, to participants: Participants = .all) async throws

Sends a standard data object asynchronously to other participants in the group session. Use this method to send data with a standard data object. Your app is responsible for decoding and using the data it receives. - Parameters: - data: The data object to send. - participants: The recipients of the message. The default value of this parameter is the set of all active participants in the session. Use the ``Participants/only(_:)-swift.enum.case`` option to specify a subset of participants.

method

send

TypeScript
sendTo78309(value: number, participants?: Participants): Promise<void>
Swift declaration
final func send<Message>(_ value: Message, to participants: Participants = .all) async throws where Message : Decodable, Message : Encodable

Sends a custom type asynchronously to other participants in the group session. Use this method to send data that you package into a custom class or structure. The method encodes your custom structure's type information, and delivers the data only to message sequences of the same type. - Parameters: - message: The custom data structure you want to send. The data structure must conform to the <doc://com.apple.documentation/documentation/swift/codable> protocol. - participants: The recipients of the message. The default value of this parameter is the set of all active participants in the session. Use the ``Participants/only(_:)-swift.enum.case`` option to specify a subset of participants.

method

messages

TypeScript
messages(type: "Bool" | "Int" | "UInt" | "Double" | "Float" | "String" | "CGFloat" | "Date" | "URL" | "Data" | "UUID"): GroupSessionMessenger_Messages<unknown>
Swift declaration
final func messages<Message>(of type: Message.Type) -> GroupSessionMessenger.Messages<Message> where Message : Decodable, Message : Encodable

Returns the asynchronous sequence of messages that match the app-specific type. Call this method to receive the messages that other participants send to the group. This method returns a ``Messages`` structure, which conforms to the `AsyncSequence` protocol. This sequence contains all, some, or none of the messages sent over time. You retrieve messages by iterating over them using an asynchronous `for`-`in` loop, as shown in the following example: ``` let sessionMessenger = GroupSessionMessenger(session: groupSession) async { for await move in sessionMessenger.messages(of: TicTacToe.Move.self) { self.board.addMove(move) } } ``` - Parameters: - type: The class of the custom type you use to package the message contents.

Skipped declarations

3 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

GroupSessionMessenger.init(session:)

This declaration needs Swift generic type arguments. (no canonical witness for Activity)

GroupSessionMessenger.init(session:deliveryMode:)

This declaration needs Swift generic type arguments. (no canonical witness for Activity)

GroupSessionMessenger.messages(of:)

Passing a Swift type itself is not supported.

All GroupActivities types →