Skip to content

A simple wrapper for the MultipeerConnectivity framework provided by Apple.

License

Notifications You must be signed in to change notification settings

benrobinson16/MPCKit

Repository files navigation

MPCKit

A simple wrapper for the MultipeerConnectivity framework provided by Apple.

Set up:

  1. Add NSLocalNetworkUsageDescription key to your Info.plist file (required on iOS 14+)
  2. Add NSBonjourServices key to your Info.plist file. Add two items to the array: _your-service._tcp and _your-service._udp
  3. Create a delegate conforming to MPCManagerDelegate protocol in order to receive updates from the MPCManager.
  4. Create an instance of class MPCManager in your app. Only one instance should be made.
import MPCKit

// Create the manager
let myManager = MPCManager()
myManager.delegate = self

// To start and stop advertising...
myManager.start(.advertising)
myManager.stop(.advertising)

// To start and stop browsing...
myManager.start(.browsing)
myManager.stop(.browsing)

// To start and stop new connections...
myManager.start(.newConnections)
myManager.stop(.newConnections)

// To start and stop all connections (including existing)...
myManager.start(.allConnections)
myManager.stop(.allConnections)

The delegate:

All changes that occur in the MPCManager class are reported via the delegate.

There are six required methods:

func foundPeer(id: MCPeerID, discoveryInfo: [String : String]?)

Notifies the delegate that a peer has been found. If you wish to invite the peer, call func invite(peer: MCPeerID) on the manager object.

func lostPeer(id: MCPeerID)

Notifies the delegate that a peer is no longer available for connection.

func connectedToPeer(id: MCPeerID)

Notifies the delegate that a peer has been successfully connected to.

func disconnectedFromPeer(id: MCPeerID)

Notifies the delegate that a peer has disconnected from the session.

func receivedInvite(from peerID: MCPeerID, context: Data?, response: @escaping (Bool) -> Void)

Notifies the delegate that an invite has been received. Call response(true) to accept or response(false) to decline.

func encounteredError(error: Error)

Notifies the delegate that an error has been encountered, providing an opportunity to present an error message to the user.

There are four optional methods:

The following methods are optional because although every use case will require at least one of these, most only require one.

func didReceive(data: Data, fromPeer id: MCPeerID)

Notifies the delegate that data has been received.

func didReceive(stream: InputStream, withName name: String, fromPeer id: MCPeerID)

Notifies the delegate that a stream has been received.

func didStartReceivingResource(withName resourceName: String, fromPeer id: MCPeerID, progress: Progress)

Notifies the delegate that resources are starting to be received.

func didFinishReceivingResource(withName resourceName: String, fromPeer id: MCPeerID, at localURL: URL?, withError error: Error?)

Notifies the delegate that resources have been delivered to the specified local URL.

License

Please see LICENSE.md

Releases

No releases published

Packages

No packages published

Languages