A Simple Tutorial for Multipeer Connectivity Framework on Swift
The Multipeer Connectivity Framework by Apple is popularly known for the discovery of nearby devices. Once this Swift framework is developed, it enables a connection that supports the communication of these services through messages, streaming, and other resources.
The two devices can connect to each other via Bluetooth or Wi-Fi. Developers can use this Swift framework to build simple games and chat applications.
When building solutions for iOS, the multipeer connectivity framework uses Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth for connectivity. But in macOS and tvOS, the framework uses Wi-Fi, Peer-to-Peer Wi-Fi, and Ethernet. So, depending on the type of connection, the mobile app development company’s approach can change.
Working with the MC Framework
The first step in the implementation of this framework is importing the same, with the import function. You will begin with;
import MultipeerConnectivity
This function allows the developer to work with Multipeer Connectivity classes and use its functions in the file.
For an application to work with the MC framework, it must interact with a specific architecture, including;
1. Peer IDs:
MCPeerID is the unique identifier of the application running on the device visible to nearby peers.
As this ID uniquely identifies a peer in the session, the users can also allocate a displayName to the ID that will be visible to the other peers on the network.
In this, the developers can set a new name for the PeerID every time they create a session. Or they can also use the same name for every session by archiving the ID at the time of its creation. They also need to unarchive the ID name the next time a session is created.
2. Session Objects:
MCSession enables communication between connected peer devices. The application will create a connection and add peers after the peers have accepted the connection request upon receiving an invitation. The purpose of the Session Objects is to maintain a set of peer ID objects associated with the peers connected on the network.
MCSession is designated with a manager class that handles and manages all the multipeer connectivity. In a session, the peers can communicate and send data with one of the following methods;
- send(_:toPeers:with:)
- session(_:didReceive:fromPeer:)
- sendResource(at:withName:toPeer:withCompletionHandler:)
Furthermore, we can also manually manage the peers, which includes establishing a connection and exchanging data.
The MCSession also has a delegate (MCSessionDelegate), which takes care of all the events related to a session.
We can use the following ways to manage all the different session states;
The MCSessionDelegate is a protocol that defines the method used by a delegate of the main session. This delegate session handles the implementation of session-related events. The delegate method handles actions like a loop or operation queue that are required to complete a session.
3. Advertiser Objects:
MCNearbyServiceAdvertiser allows a nearby peer device that the application is trying to establish a connection of a type. This object uses a single local peer connection or object to provide the required information for the identification of the device. Moreover, the Advertiser object also helps recognize other nearby devices.
4. Advertiser Assistant Objects:
MCAdvertiserAssistant works similarly to Advertiser Objects but adds another function to the mix. It also provides a standard user interface from where a user can accept the connection invite.
In addition to the interface, the Advertiser Assistant is also tasked with setting up the session and informing others about its initiation. As we use this class of functions to allocate a user interface, we can host or join a session through this class.
Following this code script, the session will be listed on the nearby devices. It will be displayed by the name we provided at the beginning of the session with MCPeerID.
5. Browser Objects:
MCNearbyServiceBrowser allows the application to search for nearby devices with applications supporting sessions. However, the sessions it supports are for a particular type.
6. Browser View Controller Objects:
MCBrowserViewController gives access to a standard user interface. With this user interface, allows a user to choose between different nearby peers and add them to a network session.
With this code script, we can allow the peers to join a session. The declaration of this function differs on iOS, iPad, tvOS and macOS. On the first three types of systems, we can use;
In addition to this, for macOS, we will use;
When a user is searching for a session to join, we need to use MCSessionViewController to display the session names and people nearby. As a result, we can allow new users to join the session. For this purpose, we need to use delegate method for MCBrowserVewController;
Working on this architecture, the MC framework works in two phases. This is the Discovery and Session phase. In the Discovery phase, the developer will use MCNearbyServiceBrowser and MCBrowserViewController . Whereas in the Session phase, the developer will use MCNearbyServiceAdvertiser and MCAdvertiserAssistant.
While using the MC framework, the developers will find it easier to implement the entire structure, even though it may look a bit complex. We will help you understand the implementation of this framework.
Sending and Receiving Data on Peer-to-Peer Session
While we have learned how to establish a connection, allocate a unique name, and allow users to view and join a session on a Swift framework-developed application. Now let’s check out how to send and receive data in the session.
First, we need to identify the data that is to be sent through the session and when. While the users can do the same on the click of a button, here’s the code script to execute;
For sending data, it must be formatted to the type Data(). For instance, if you wish to send images, a good way to represent the same will be .pngData() method.
At the same time, we need to implement a code script for receiving data from within the session. Most of the time, the device on which a user is receiving the data understands what to do with the same.
However, in a MC Framework, how the received data will be treated depends on the type of data. We can use the following method to decide what happens with the data received on a device;
We can use the multipeer connectivity framework to establish a connection between different peers and devices.
Conclusion
The MC framework is a communication method to connect different iOS devices and transmit data between the same. This framework provides a layer that works on top of the Bonjour Protocol.
However for this to work on top of the Bonjour Protocol, we need to declare the services with the NSBonjourServices key.
Apple introduced this framework to help an application discover what’s around it and open connections on both sides. Learning about this framework is essential for an iOS application development company. OS app development company as it will prove to be helpful for creating better applications.