|
| 1 | +# ``MultipeerKit`` |
| 2 | + |
| 3 | +A high-level abstraction built on top of the MultipeerConnectivity framework, which allows iOS, macOS and tvOS devices to exchange data between them over Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +MultipeerKit is an abstraction on top of the MultipeerConnectivity framework, which enables peer-to-peer communication of Apple devices over WiFi. |
| 8 | + |
| 9 | +With MultipeerKit, you can exchange messages between Apple devices running your app, the message can be anything that implements the `Codable` protocol. |
| 10 | + |
| 11 | +## Getting Started |
| 12 | + |
| 13 | +### Info.plist Requirements |
| 14 | + |
| 15 | +Starting with iOS 14, you will have to include two keys in your app's Info.plist file in order for MultipeerKit to work properly. |
| 16 | + |
| 17 | +The keys are `Privacy - Local Network Usage Description` (`NSLocalNetworkUsageDescription`) and `Bonjour services` (`NSBonjourServices`). |
| 18 | + |
| 19 | +For the privacy key, include a human-readable description of what benefit the user gets by allowing your app to access devices on the local network. |
| 20 | + |
| 21 | +The Bonjour services key is an array of service types that your app will browse for. For MultipeerKit, the entry should be in the format `_servicename._tcp`, where `servicename` is the `serviceType` you've set in your `MultipeerConfiguration`. If you're using the default configuration, the value of this key should be `_MKSVC._tcp`. |
| 22 | + |
| 23 | +### Sending and Receiving Messages |
| 24 | + |
| 25 | +The main class in this library is ``MultipeerTransceiver``, which does both the sending and receiving aspects of the multipeer communication. |
| 26 | + |
| 27 | +MultipeerKit can transmit and receive anything that conforms to the `Codable` protocol, which makes it easy for you to define your own message types. |
| 28 | + |
| 29 | +```swift |
| 30 | +// Create a transceiver (make sure you store it somewhere, like a property) |
| 31 | +let transceiver = MultipeerTransceiver() |
| 32 | + |
| 33 | +// Start it up! |
| 34 | +transceiver.resume() |
| 35 | + |
| 36 | +// Configure message receivers |
| 37 | +transceiver.receive(SomeCodableThing.self) { payload, sender in |
| 38 | + print("Got my thing from \(sender.name)! \(payload)") |
| 39 | +} |
| 40 | + |
| 41 | +// Broadcast message to peers |
| 42 | +let payload = SomeEncodableThing() |
| 43 | +transceiver.broadcast(payload) |
| 44 | +``` |
0 commit comments