PeerConnectionManager

public class PeerConnectionManager

Functional wrapper for Apple’s MultipeerConnectivity framework.

Initialize a PeerConnectionManager to enable mesh-networking over bluetooth and wifi when available. Configure the networking protocol of the session and then start to begin connecting to nearby peers.

  • Access to shared connection managers by their service type.

    Declaration

    Swift

    public fileprivate(set) static var shared : [ServiceType:PeerConnectionManager] = [:]
  • The connection type for the connection manager. (ex. .automatic, .inviteOnly, .custom)

    Declaration

    Swift

    public let connectionType : PeerConnectionType
  • Access to the local peer representing the user.

    Declaration

    Swift

    public let peer : Peer
  • Returns the peers that are connected on the current session.

    Declaration

    Swift

    public var connectedPeers : [Peer]
  • Nearby peers available for connecting.

    Declaration

    Swift

    public fileprivate(set) var foundPeers: [Peer] = []
  • Initializer for a connection manager. Requires the requested service type. If the connectionType and displayName are not specified the connection manager defaults to .Automatic and using the local device name.

    Returns

    A fully initialized PeerConnectionManager.

    Declaration

    Swift

    public init(serviceType: ServiceType,
                    connectionType: PeerConnectionType = .automatic,
                    displayName: String = UIDevice.current.name)

    Parameters

    serviceType

    The requested service type describing the channel on which peers are able to connect.

    connectionType

    Takes a PeerConnectionType case determining the default behavior of the framework.

    displayName

    The local user’s display name to other peers.

    Return Value

    A fully initialized PeerConnectionManager.

  • Start the connection manager with optional completion. Calling this initiates browsing and advertising using the specified connection type.

    Declaration

    Swift

    public func start(_ completion: ((Void)->Void)? = nil)

    Parameters

    completion

    Called once session is initialized. Default is nil.

  • Returns a browser view controller if the connectionType was set to .InviteOnly or returns nil if not.

    Returns

    A browser view controller for inviting available peers nearby if connection type is .InviteOnly or nil otherwise.

    Declaration

    Swift

    public func browserViewController(_ callback: @escaping (PeerBrowserViewControllerEvent)->Void) -> UIViewController?

    Parameters

    callback

    Events sent back with cases .DidFinish and .DidCancel.

    Return Value

    A browser view controller for inviting available peers nearby if connection type is .InviteOnly or nil otherwise.

  • Use to invite peers that have been found locally to join a MultipeerConnectivity session.

    Declaration

    Swift

    public func invitePeer(_ peer: Peer, withContext context: Data? = nil, timeout: TimeInterval = 30)

    Parameters

    peer

    Peer object to invite to current session.

    withContext

    Data object associated with the invitation.

    timeout

    Time interval until the invitation expires.

  • Send data to connected users. If no peer is specified it broadcasts to all users on a current session.

    Declaration

    Swift

    public func sendData(_ data: Data, toPeers peers: [Peer] = [])

    Parameters

    data

    Data to be sent to specified peers.

    toPeers

    Specified Peer objects to send data.

  • Send events to connected users. Encoded as Data using the NSKeyedArchiver. If no peer is specified it broadcasts to all users on a current session.

    Declaration

    Swift

    public func sendEvent(_ eventInfo: [String:Any], toPeers peers: [Peer] = [])

    Parameters

    eventInfo

    Dictionary of Any data which is encoded with the NSKeyedArchiver and passed to the specified peers.

    toPeers

    Specified Peer objects to send event.

  • Send a data stream to a connected user. This method throws an error if the stream cannot be established. This method returns the NSOutputStream with which you can send events to the connected users.

    Throws

    Propagates errors thrown by Apple’s MultipeerConnectivity framework.

    Returns

    The OutputStream for sending information to the specified Peer object.

    Declaration

    Swift

    public func sendDataStream(streamName name: String, toPeer peer: Peer) throws -> OutputStream

    Parameters

    streamName

    The name of the stream to be established between two users.

    toPeer

    The peer with which to start a data stream

    Return Value

    The OutputStream for sending information to the specified Peer object.

  • Send a resource with a specified url for retrieval on a connected device. This method can send a resource to multiple peers and returns an Progress associated with each Peer. This method takes an error completion handler if the resource fails to send.

    Returns

    A dictionary of optional Progress associated with each peer that the resource was sent to.

    Declaration

    Swift

    public func sendResourceAtURL(_ resourceURL: URL, withName name: String, toPeers peers: [Peer] = [], withCompletionHandler completion: ((Error?) -> Void)? ) -> [Peer:Progress?]

    Parameters

    resourceURL

    The url that the resource will be passed with for retrieval.

    withName

    The name with which the progress is associated with.

    toPeers

    The specified peers for the resource to be sent to.

    withCompletionHandler

    the completion handler called when an error is thrown sending a resource.

    Return Value

    A dictionary of optional Progress associated with each peer that the resource was sent to.

  • Refresh the current session. This call disconnects the user from the current session and then restarts the session with completion maintaing the current sessions configuration.

    Declaration

    Swift

    public func refresh(_ completion: ((Void)->Void)? = nil)

    Parameters

    completion

    Completion handler called after the session has completed refreshing.

  • Stop the current connection manager from listening to delegate callbacks and disconnects from the current session.

    Declaration

    Swift

    public func stop()
  • Close session for browsing peers to invite.

    Declaration

    Swift

    public func closeSession()
  • Open session for browsing peers to invite.

    Declaration

    Swift

    public func openSession()
  • Takes a PeerConnectionEventListener to respond to events.

    Declaration

    Swift

    public func listenOn(_ listener: @escaping PeerConnectionEventListener, performListenerInBackground background: Bool = false, withKey key: String)

    Parameters

    listener

    Takes a PeerConnectionEventListener.

    performListenerInBackground

    Default is false. Set to true to perform the listener asyncronously.

    withKey

    The key with which to associate the listener.

  • Takes a key to register the callback and calls the listener when an event is recieved and also passes back the Peer that sent it.

    Declaration

    Swift

    public func observeEventListenerForKey(_ key: String, listener: @escaping ([String:Any], Peer)->Void)

    Parameters

    key

    String key with which to keep track of the listener for later removal.

    listener

    Callback that returns the event info and the Peer whenever an event is received.

  • Remove a listener associated with a specified key.

    Declaration

    Swift

    public func removeListenerForKey(_ key: String)

    Parameters

    key

    The key with which to attempt to find and remove a listener with.

  • Remove all listeners.

    Declaration

    Swift

    public func removeAllListeners()