MultipeerKit Documentation Beta

Class Multipeer​Transceiver

public final class MultipeerTransceiver

Handles all aspects related to the multipeer communication.

Initializers

init(configuration:​)

public init(configuration: MultipeerConfiguration = .default)

Initializes a new transceiver.

Parameters

configuration Multipeer​Configuration

The configuration, uses the default configuration if none specified.

Properties

available​Peers​Did​Change

var availablePeersDidChange: ([Peer]) -> Void

Called on the main queue when available peers have changed (new peers discovered or peers removed).

peer​Added

var peerAdded: (Peer) -> Void

Called on the main queue when a new peer discovered.

peer​Removed

var peerRemoved: (Peer) -> Void

Called on the main queue when a peer removed.

available​Peers

var availablePeers: [Peer]

All peers currently available for invitation, connection and data transmission.

Methods

receive(_:​using:​)

public func receive<T: Codable>(_ type: T.Type, using closure: @escaping (_ payload: T) -> Void)

Configures a new handler for a specific Codable type.

MultipeerKit communicates data between peers as JSON-encoded payloads which originate with Codable entities. You register a closure to handle each specific type of entity, and this closure is automatically called by the framework when a remote peer sends a message containing an entity that decodes to the specified type.

Parameters

type T.​Type

The Codable type to receive.

closure @escaping (_ payload:​ T) -> Void

The closure that will be called whenever a payload of the specified type is received.

payload

The payload decoded from the remote message.

resume()

public func resume()

Resumes the transceiver, allowing this peer to be discovered and to discover remote peers.

stop()

public func stop()

Stops the transceiver, preventing this peer from discovering and being discovered.

broadcast(_:​)

public func broadcast<T: Encodable>(_ payload: T)

Sends a message to all connected peers.

Parameters

payload T

The payload to be sent.

send(_:​to:​)

public func send<T: Encodable>(_ payload: T, to peers: [Peer])

Sends a message to a specific peer.

Parameters

payload T

The payload to be sent.

peers [Peer]

An array of peers to send the message to.

invite(_:​with:​timeout:​completion:​)

public func invite(_ peer: Peer, with context: Data?, timeout: TimeInterval, completion: InvitationCompletionHandler?)

Manually invite a peer for communicating.

You can call this method to manually invite a peer for communicating if you set the invitation parameter to .none in the transceiver's configuration.

Parameters

peer Peer

The peer to be invited.

context Data?

Custom data to be sent alongside the invitation.

timeout Time​Interval

How long to wait for the remote peer to accept the invitation.

completion Invitation​Completion​Handler?

Called when the invitation succeeds or fails.