Giter Site home page Giter Site logo

benrobinson16 / mpckit Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 21 KB

A simple wrapper for the MultipeerConnectivity framework provided by Apple.

License: MIT License

Swift 100.00%
multipeer-connectivity swift wrapper lightweight multipeerconnectivity swift-library swift-package multipeerconnectivity-framework

mpckit's Introduction

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

mpckit's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.