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
  • configuration: The configuration, uses the default configuration if none specified.

init(connection:​)

init(connection: MultipeerProtocol)

Properties

log

let log

connection

let connection: MultipeerProtocol

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.

local​Peer​Id

var localPeerId: String?

The current device's peer id

available​Peers

var availablePeers: [Peer]

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

Methods

configure(_:​)

private func configure(_ connection: MultipeerProtocol)

receive(_:​using:​)

public func receive<T: Codable>(_ type: T.Type, using closure: @escaping (_ payload: T, _ sender: Peer) -> 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
  • type: The Codable type to receive.
closure @escaping (_ payload:​ T, _ sender:​ Peer) -> Void
  • closure: The closure that will be called whenever a payload of the specified type is received.
payload
  • payload: The payload decoded from the remote message.
sender
  • sender: The remote peer who sent the 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
  • payload: 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
  • payload: The payload to be sent.
peers [Peer]
  • peers: An array of peers to send the message to.

handle​Data​Received(_:​from:​)

private func handleDataReceived(_ data: Data, from peer: Peer)

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
  • peer: The peer to be invited.
context Data?
  • context: Custom data to be sent alongside the invitation.
timeout Time​Interval
  • timeout: How long to wait for the remote peer to accept the invitation.
completion Invitation​Completion​Handler?
  • completion: Called when the invitation succeeds or fails.

handle​Peer​Added(_:​)

private func handlePeerAdded(_ peer: Peer)

handle​Peer​Removed(_:​)

private func handlePeerRemoved(_ peer: Peer)

handle​Peer​Connected(_:​)

private func handlePeerConnected(_ peer: Peer)

handle​Peer​Disconnected(_:​)

private func handlePeerDisconnected(_ peer: Peer)

set​Connected(_:​on:​)

private func setConnected(_ connected: Bool, on peer: Peer)