BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

Going peer to peer: MCSession, MCBrowserViewController

The next step is to add a left bar button item to our view controller, using the "add" system icon, and making it call a method called showConnectionPrompt(). We're going to make that method ask users whether they want to connect to an existing session with other people, or whether they want to create their own. Here's the code for the bar button item – put this in viewDidLoad():

navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(showConnectionPrompt))

Asking users to clarify how they want to take an action is of course the purpose of UIAlertController as an action sheet, and our showConnectionPrompt() method is going to use one to ask users what kind of connection they want to make. Put this code into your view controller:

@objc func showConnectionPrompt() {
    let ac = UIAlertController(title: "Connect to others", message: nil, preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "Host a session", style: .default, handler: startHosting))
    ac.addAction(UIAlertAction(title: "Join a session", style: .default, handler: joinSession))
    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
    present(ac, animated: true)
}

Now, here's where it gets trickier. Multipeer connectivity requires four new classes:

  1. MCSession is the manager class that handles all multipeer connectivity for us.
  2. MCPeerID identifies each user uniquely in a session.
  3. MCAdvertiserAssistant is used when creating a session, telling others that we exist and handling invitations.
  4. MCBrowserViewController is used when looking for sessions, showing users who is nearby and letting them join.

We're going to use all four of them in our app, but only three need to be properties.

Start by importing the multipeer framework:

import MultipeerConnectivity

Now add these to your view controller:

var peerID = MCPeerID(displayName: UIDevice.current.name)
var mcSession: MCSession?
var mcAdvertiserAssistant: MCAdvertiserAssistant?

Although both the session and advertiser assistant are optional, that code creates the MCPeerID up front using the name of the current device, which will usually be something like "Paul's iPhone" – there’s no need to make an optional for that.

Depending on what users select in our alert controller, we need to call one of two methods: startHosting() or joinSession(). Because both of these are coming from the result of a UIAction being tapped, both methods must accept a UIAlertAction as their only parameter.

Before I show you the code to get multipeer connectivity up and running, I want to go over what they will do. Most important of all is that all multipeer services on iOS must declare a service type, which is a 15-digit string that uniquely identify your service. Those 15 digits can contain only the letters A-Z, numbers and hyphens, and it's usually preferred to include your company in there somehow.

Apple's example is, "a text chat app made by ABC company could use the service type abc-txtchat"; for this project I'll be using hws-project25.

This service type is used by both MCAdvertiserAssistant and MCBrowserViewController to make sure your users only see other users of the same app. They both also want a reference to your MCSession instance so they can take care of connections for you.

We're going to start by initializing our MCSession so that we're able to make connections. Put this code into viewDidLoad():

mcSession = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .required)
mcSession?.delegate = self

Our peer ID is used to create the session, along with the encryption option of .required to ensure that any data transferred is kept safe.

Don't worry about conforming to any extra protocols just yet; we'll do that in just a minute.

At this point, the code for startHosting() and joinSession() will look quite trivial. Here goes:

func startHosting(action: UIAlertAction) {
    guard let mcSession = mcSession else { return }
    mcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "hws-project25", discoveryInfo: nil, session: mcSession)
    mcAdvertiserAssistant?.start()
}

func joinSession(action: UIAlertAction) {
    guard let mcSession = mcSession else { return }
    let mcBrowser = MCBrowserViewController(serviceType: "hws-project25", session: mcSession)
    mcBrowser.delegate = self
    present(mcBrowser, animated: true)
}

We're making our view controller the delegate of a second object, so that's two protocols we need to conform to in order to fix our current compile failures. Easily done: add MCSessionDelegate and MCBrowserViewControllerDelegate to your class definition… and now there are even more errors, because we need to implement lots of new methods.

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.