Wrap up
Wrap up êŽë š
Multipeer connectivity is something that used to be awfully hard, but in iOS it's less than 150 lines of code to produce this entire app â and over half of that is code for the collection view and the image picker!
The advantage it has compared to traditional data sharing over Wi-Fi is that multipeer can use an existing Wi-Fi network, or can silently create a new Wi-Fi network or even a Bluetooth network depending on what's available. All this is an implementation detail that Apple solves on your behalf â we donât have to care how it works.
Review what you learned
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. Itâs my job to make sure you take as much from these tutorials as possible, so Iâve prepared a short review to help you check your learning.
Challenge
One of the best ways to learn is to write your own code as often as possible, so here are three ways you should try your new knowledge to make sure you fully understand whatâs going on:
- Show an alert when a user has disconnected from our multipeer network. Something like âPaulâs iPhone has disconnectedâ is enough.
- Try sending text messages across the network. You can create a
Data
from a string usingData(yourString.utf8)
, and convert aData
back to a string by usingString(decoding: yourData, as: UTF8.self)
. - Add a button that shows an alert controller listing the names of all devices currently connected to the session â use the
connectedPeers
property of your session to find that information.