Multipeer Connectivity on iOS 8 with Swift

Discover, connect, and transmit data between devices.

portable_handsetMultipeer Connectivity is a means of identifying and connecting services together, and allows for very easy passing back and forth of data with minimal setup. If you are thinking that this seems similar to Bonjour, you are correct; however, Bonjour is for service discovery, not connection or transmission. Multipeer Connectivity handles everything in one neat package.

Multipeer Connectivity is comprised of a few different components, but it works by having applications advertise their services asking if anyone is available to connect. The service browser listens for these advertisements and can request to create a connection between the devices. If the connection is accepted, a session is created with every one inside the session being represented by a peer object.

To demonstrate how this works, we’ll make a simple chat application using Swift, Apple’s new language.

Note that this exercise is written for iOS 8, but the Multipeer Connectivity framework also works on OS X.

  1. Create a new Single View iPhone application called MultipeerChat.
  2. Open the project in the project navigator and the add the MultipeerConnectivity framework into the project from the Linked Frameworks and Libraries section.
  3. Open the Main.storyboard, and add a UITextView. Make it take up most of the space, and set its editable field to be false in the Attributes Inspector.
  4. Add a UITextField and add two buttons. Label one Browse and the other Send.
  5. Connect the UITextView up to a property called chatView.
  6. Connect the UITextField up to a property called messageField.
  7. Connect the browse button up to an action called showBrowser, and the Send button up to an action called sendChat.

When complete, your interface should look like the following image.

multipeer-ui

Modify the ViewController so that is looks like the following:

This code does the following things:

  1. It adds a class extension to the ViewController class, which makes
    it conform to the MCBrowserViewControllerDelegate and
    MCSessionDelegate protocols, as well as creating four new properties,
    including one to handle the Multicast Peer components:

    • The MCBrowserViewController is a prebuilt view controller for handling and negotiating browsing for connections.
    • The MCAdvertiserAssistant is another prebuilt class for both advertising and negotiating creating a connection and session.
    • The MCSession is the object holding the sessions once it has been negotiated.
    • MCPeerID represents your peer id for the session.
  2. In the viewDidLoad method, the code initializes all the Multicast Peer objects before telling the advertiser to start advertising its availability.
  3. In the sendChat method, the sendData(toPeers:error:) message on the session is the real meat of the application. This is the method used to send data—in our application, a string from the text field.
  4. Finally, session(didReceiveData:fromPeer:) is the delegate method that is called when the session receives any data—in the case of our application. It appends the new data to the text view, showing the received text to the text view.

Now if you and another friend both (or you, using two devices) run the application, you should have an amazing chat app!

Editor’s note: For a limited time, you can save 50% on “Swift Development with Cocoa” and other iOS 8 titles at shop.oreilly.com.

Public domain handset illustration courtesy of Internet Archive.

tags: , , , , ,