MultiPeer

public class MultiPeer : NSObject
extension MultiPeer: MCNearbyServiceAdvertiserDelegate
extension MultiPeer: MCNearbyServiceBrowserDelegate
extension MultiPeer: MCSessionDelegate

Main Class for MultiPeer

  • Singleton instance - call via MultiPeer.instance

    Declaration

    Swift

    public static let instance: MultiPeer

Properties

  • Conforms to MultiPeerDelegate: Handles receiving data and changes in connections

    Declaration

    Swift

    public weak var delegate: MultiPeerDelegate?
  • Amount of time to spend connecting before timeout

    Declaration

    Swift

    public var connectionTimeout: Double
  • Peers available to connect to

    Declaration

    Swift

    public var availablePeers: [Peer]
  • Peers connected to

    Declaration

    Swift

    public var connectedPeers: [Peer]
  • Names of all connected devices

    Declaration

    Swift

    public var connectedDeviceNames: [String] { get }
  • Prints out all errors and status updates

    Declaration

    Swift

    public var debugMode: Bool

Initializers

  • Initializes the MultiPeer service with a serviceType and the default deviceName

    Declaration

    Swift

    public func initialize(serviceType: String)

    Parameters

    serviceType

    String with name of MultiPeer service. Up to one hyphen (-) and 15 characters. Uses default device name

  • Initializes the MultiPeer service with a serviceType and a custom deviceName

    Declaration

    Swift

    public func initialize(serviceType: String, deviceName: String)

    Parameters

    serviceType

    String with name of MultiPeer service. Up to one hyphen (-) and 15 characters.

    deviceName

    String containing custom name for device

Methods

  • HOST: Automatically browses and invites all found devices

    Declaration

    Swift

    public func startInviting()
  • JOIN: Automatically advertises and accepts all invites

    Declaration

    Swift

    public func startAccepting()
  • HOST and JOIN: Uses both advertising and browsing to connect.

    Declaration

    Swift

    public func autoConnect()
  • Stops the invitation process

    Declaration

    Swift

    public func stopInviting()
  • Stops accepting invites and becomes invisible on the network

    Declaration

    Swift

    public func stopAccepting()
  • Stops all invite/accept services

    Declaration

    Swift

    public func stopSearching()
  • Disconnects from the current session and stops all searching activity

    Declaration

    Swift

    public func disconnect()
  • Stops all invite/accept services, disconnects from the current session, and stops all searching activity

    Declaration

    Swift

    public func end()
  • Returns true if there are any connected peers

    Declaration

    Swift

    public var isConnected: Bool { get }
  • Sends an object (and type) to all connected peers.

    Declaration

    Swift

    public func send(object: Any, type: UInt32)

    Parameters

    object

    Object (Any) to send to all connected peers.

    type

    Type of data (UInt32) sent After sending the object, you can use the extension for Data, convertData() to convert it back into an object.

  • Sends Data (and type) to all connected peers.

    Declaration

    Swift

    public func send(data: Data, type: UInt32)

    Parameters

    data

    Data (Data) to send to all connected peers.

    type

    Type of data (UInt32) sent After sending the data, you can use the extension for Data, convertData() to convert it back into data.

Advertiser Delegate

Browser Delegate

  • Found a peer, update the list of available peers

    Declaration

    Swift

    public func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?)
  • Lost a peer, update the list of available peers

    Declaration

    Swift

    public func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID)
  • Error, could not start browsing

    Declaration

    Swift

    public func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error)

Session Delegate