A wrapper for Apple’s MultipeerConnectivity framework for offline data transmission between Apple devices. This framework makes it easy to automatically connect to multiple nearby devices and share information using either bluetooth or wifi radios.
Then, simply initialize MultiPeer with the name of your session (serviceType). There are two modes of connections (advertiser and browser). To utilize both, simply use .autoConnect().
Any data transmitted by MultiPeer will always be accompanied by a numerical “type”, to ensure other peers know what kind of data is being received, and how to properly process it. You can manage this by creating a UInt32 enum, as shown below:
enum DataType: UInt32 {
case string = 1
case image = 2
// ...
}
To send data, simply use the .send(object: type:) function:
To receive data, we must conform to the MultiPeerDelegate protocol:
func multiPeer(didReceiveData data: Data, ofType type: UInt32, from peerID: MCPeerID) {
switch type {
case DataType.string.rawValue:
let string = data.convert() as! String
// do something with the received string
break
case DataType.image.rawValue:
let image = UIImage(data: data)
// do something with the received UIImage
break
default:
break
}
}
func multiPeer(connectedDevicesChanged devices: [String]) {
}
Ensure that you set the MultiPeer delegate.
MultiPeer.instance.delegate = self
Finally you’ll need to enable incoming / outgoing connections in your entitlements.
Congratulations! You have successfully sent data using MultiPeer! For more detailed information (including details of other functions), please see the docs.
A wrapper for Apple’s MultipeerConnectivity framework for offline data transmission between Apple devices. This framework makes it easy to automatically connect to multiple nearby devices and share information using either bluetooth or wifi radios.
Features
Integration
CocoaPods
You can use CocoaPods to install
MultiPeer
by adding it to yourPodfile
:Carthage
You can use Carthage to install
MultiPeer
by adding it to yourCartfile
:Swift Package Manager
For SPM, add the following to your package dependencies:
Usage
To get started, import MultiPeer.
Then, simply initialize MultiPeer with the name of your session (
serviceType
). There are two modes of connections (advertiser
andbrowser
). To utilize both, simply use.autoConnect()
.Any data transmitted by MultiPeer will always be accompanied by a numerical “type”, to ensure other peers know what kind of data is being received, and how to properly process it. You can manage this by creating a
UInt32
enum, as shown below:To send data, simply use the
.send(object: type:)
function:To receive data, we must conform to the
MultiPeerDelegate
protocol:Ensure that you set the MultiPeer delegate.
Finally you’ll need to enable incoming / outgoing connections in your entitlements.
Congratulations! You have successfully sent data using MultiPeer! For more detailed information (including details of other functions), please see the docs.
Example
For an example app using MultiPeer, checkout MultiPeer_Sample.
License
MultiPeer
is released under an MIT License. See LICENSE for details.Authors
Project heavily inspired by Apple-Signal.