SmartMultipeer
Example
To run the example project, clone the repo, and run from the Example directory first.
Requirements
iOS Version required is 11.0
Installation
SmartMultipeer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SmartMultipeer'
Usage
- Import the module
import SmartMultipeer
Classes & Protocol
You need to use PeerConnectivity.swift
file that manages everything and contains all required methods. DataSync.swfit
protocol contains DataSyncDelegate that needs to be implemented in class to receive data. It contains 4 methods that are:
func sync(dataDidReceive: Any, ofType: Type, at: Date)
Method is called when data is received on other device
func update(devices: [PeerDevice], at: Date)
Method is called when any device is added / lost
func lost(device: PeerDevice, at: Date)
Method is called when any device is lost. This method is optional
func acknowledge(from: PeerDevice, at: Date, forDataID: Any)
Method is called when data is received and sender will be acknowledged. This method is optional
How to start
- Initialize service type by using:
PeerConnectivity.instance.service = "sample-service"
- Setup Multipeer Connectivity
PeerConnectivity.instance.setup(fromViewController: self, withDelegate: self)
- Connect Users
a. Individually
PeerConnectivity.instance.connect(forUser: .Peer)
PeerConnectivity.instance.connect(forUser: .Host)
b. Simultaneously
PeerConnectivity.instance.autoConnect()
- To send data to connected peers
func send(data: Any, ofType: Type, withID: Any)
You can send data of any type whether its primitive or custom model that is designed or encapsulated in class and 'ofType' that can contain values defined in Type.swift
enum. 'withID' can be used to validated at the time of acknowledgement.
Other helper methods & properties
PeerConnectivity.instance.disconnect()
Disconnect all peers and will stop advertising and browsing
PeerConnectivity.instance.getAllRegisteredDevices()
Returns the history of devices connected up till now
PeerConnectivity.instance.peer
Returns the information of current device
PeerConnectivity.instance.getAvailablePeers()
Return all available peers ready to get connected
PeerConnectivity.instance.getConnectedPeers()
Returns all connected peers
Author
AJ Sagar Parwani Email: [email protected]
License
SmartMultipeer is available under the Author's license. See the LICENSE file for more info.