A simple wrapper for the MultipeerConnectivity framework provided by Apple.
- Add
NSLocalNetworkUsageDescription
key to yourInfo.plist
file (required on iOS 14+) - Add
NSBonjourServices
key to yourInfo.plist
file. Add two items to the array:_your-service._tcp
and_your-service._udp
- Create a delegate conforming to
MPCManagerDelegate
protocol in order to receive updates from theMPCManager
. - 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)
All changes that occur in the MPCManager class are reported via the delegate.
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.
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.
Please see LICENSE.md