<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joe Diragi</title>
    <description>The latest articles on DEV Community by Joe Diragi (@joe_diragi_3bb3b9c26bddca).</description>
    <link>https://dev.to/joe_diragi_3bb3b9c26bddca</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F901021%2Fc992705e-0274-44ac-a802-5050fb754ea3.png</url>
      <title>DEV Community: Joe Diragi</title>
      <link>https://dev.to/joe_diragi_3bb3b9c26bddca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joe_diragi_3bb3b9c26bddca"/>
    <language>en</language>
    <item>
      <title>The horrors of Multipeer Connectivity and SwiftUI 4</title>
      <dc:creator>Joe Diragi</dc:creator>
      <pubDate>Sun, 31 Jul 2022 19:15:38 +0000</pubDate>
      <link>https://dev.to/joe_diragi_3bb3b9c26bddca/the-horrors-of-multipeer-connectivity-and-swiftui-4-mkb</link>
      <guid>https://dev.to/joe_diragi_3bb3b9c26bddca/the-horrors-of-multipeer-connectivity-and-swiftui-4-mkb</guid>
      <description>&lt;p&gt;Multipeer Connectivity is a technology released by Apple at their 2014 (I think) WWDC. Multipeer Connectivity (MPC for short) is a part of Apple's Nearby Interaction framework that aims to make direct communication between devices as seamless sand secure as possible. When I set out to create a rock, paper, scissors game that utilized this framework I thought I'd be in for a mostly painless learning experience. To be fair, if this framework had been &lt;em&gt;updated&lt;/em&gt; to support SwiftUI I really would not have had a single problem. From what I read on StackOverflow and the Apple developer forums, this framework is really pretty simple to use. That being said, it is clear the framework was designed to work with UIKit and hasn't been updated in a while.&lt;br&gt;
You might be thinking to yourself, "What does UIKit/SwiftUI have to do with a peer-to-peer communication framework?", and you'd be correct in doing so. The thing is, SwiftUI is also a much different programming paradigm than the old way of doing things. SwiftUI's addition of things like &lt;code&gt;StateObject&lt;/code&gt;s and &lt;code&gt;EnvironmentVariable&lt;/code&gt;s really muddy the water when compared to the way apps written with &lt;code&gt;UIKit&lt;/code&gt; were built.&lt;br&gt;
I started building the app with the intention of creating a YouTube tutorial, since I read doing so can be a great way to hone ones skills and potentially build a following and further ones career. I dove right into the documentation and things were going smooth. It turned out there are 2 main ways to go about creating an app that utilizes MPC: use an &lt;code&gt;MCAdvertiserAssistant&lt;/code&gt; to handle all of the dirty work with pairing, or use the &lt;code&gt;MCNearbyServiceAdvertiser&lt;/code&gt; to implement the logic yourself.&lt;br&gt;
When I read through the docs I was confident my use case only required the use of the simplified &lt;code&gt;MCAdvertiserAssistant&lt;/code&gt;, which can be used as simply as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let peerID = MCPeerID(displayName: "Username")
let session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .none)
let assistant = MCAdvertiserAssistant(serviceType: "rps-service", discoveryInfo: nil, session: session)
assistant.start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, the MCAdvertiserAssistant would take care of the hard work for us. I implemented a nearby device browser like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let serviceBrowser = MCNearbyServiceBrowser(peer: peerID, serviceType: "rps-service")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once that was setup I assigned a delegate to the service browser to find available peers and add them to a &lt;code&gt;@Published&lt;/code&gt; property that my view could use to allow the player to invite a peer to a game. Once the other player received the invitation, the &lt;code&gt;MCAdvertiserAssistant&lt;/code&gt; would take care of the rest.&lt;br&gt;
I deployed the app on my iPhone and my MacBook, created usernames for both and immediately I could see the other player on my device. It was working great! When I selected the MacBook player from my iPhone, a dialog was displayed almost instantly asking if I wanted to pair with the iPhone and start a game. When I selected yes I was brought into the game and everything was working perfectly.&lt;br&gt;
That was all finished and working in a matter of hours, pairing, peer-to-peer messaging and gameplay done!&lt;br&gt;
Or so I thought. I went back to test pairing in the opposite direction, selected the iPhone from my MacBook and....&lt;br&gt;
Nothing!&lt;br&gt;
Xcode spat out some crazy error regarding the alert that the &lt;code&gt;MCAdvertiserAssistant&lt;/code&gt; was trying to display. I spent &lt;em&gt;hours&lt;/em&gt; searching through any StackOverflow thread that mentioned MultiPeer Connectivity and SwiftUI. All of the accepted answers were fixes that only worked with UIKit applications, and half of the reason I was creating the tutorial in the first place was to show the basics of SwiftUI development. &lt;br&gt;
Finally it occurred to me that I would have to ditch the &lt;code&gt;MCAdvertiserAssistant&lt;/code&gt; and implement my own pairing logic using &lt;code&gt;MCNearbyServiceAdvertiser&lt;/code&gt; (🤢).&lt;br&gt;
The &lt;code&gt;MCNearbyServiceAdvertiserDelegate&lt;/code&gt; has two functions, one gets called when the advertiser cannot start advertising and the other is called when it receives an invitation from a peer. The problem now was figuring out how to show an alert inside of my view from within my &lt;code&gt;RPSMultipeerSession&lt;/code&gt; class. &lt;br&gt;
In SwiftUI, alerts are essentially a property of a view. They are shown like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@State var showAlert: Bool = false
...
HStack {
    ...
}
.alert("Title", isPresented: $showAlert) {
    Button("Action 1") {
        ...
    }
}
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;showAlert&lt;/code&gt; in the above example is set to true, the alert is shown. I figured I could use a published variable inside of &lt;code&gt;RPSMultipeerSession&lt;/code&gt; to show the alert when an invitation is received inside of the &lt;code&gt;MCNearbyServiceAdvertiserDelegate&lt;/code&gt;. That worked just fine, however, in order to accept the invitation, one must call the &lt;code&gt;invitationHandler&lt;/code&gt; passed into the function inside of the delegate. In other words, there was no obvious way to accept the invitation from inside of the view.&lt;/p&gt;

&lt;p&gt;This is the ugly part. I essentially had to hack together a solution here by creating &lt;em&gt;another&lt;/em&gt; published variable in the &lt;code&gt;RPSMultipeerSession&lt;/code&gt; class that holds the &lt;code&gt;invitationHandler&lt;/code&gt; from the &lt;code&gt;MCNearbyServiceDelegate&lt;/code&gt;'s &lt;code&gt;didReceiveInvitationFromPeer&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;Here's some code ❤️&lt;/p&gt;

&lt;p&gt;The fun part is of course in the &lt;code&gt;MCNearbyServiceAdvertiserDelegate&lt;/code&gt;. Specifically this method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -&amp;gt; Void {
    DispatchQueue.main.async {
        self.recvdInvite = true
        self.invitationHandler = invitationHandler
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we update the published variable to alert the UI that we received an invite and it should show an alert. This is what the UI looks like (give or take):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct PairView: View {
    @StateObject var rpsSession: RPSMultipeerSession

    var body: some View {
        if (!rpsSession.paired) {
            HStack {
                List(rpsSession.availablePeers, id: \.self) { peer in
                    Button(peer.displayName) {
                        rpsSession.serviceBrowser.invitePeer(peer, to: rpsSession.session, withContext: nil, timeout: 20)
                    }
                }
            }
            .alert("Received an invite", isPresented: $rpsSession.recvdInvite) {
                Button("Accept") {
                    if (rpsSession.invitationHandler != nil) {
                        rpsSession.invitationHandler!(true, rpsSession.session)
                    }
                }
            }
        } else {
            GameView()
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With all of that in place, pairing &lt;em&gt;finally&lt;/em&gt; works correctly both ways between iOS and macOS. If you'd like to take a look at the complete source code, it's on &lt;a href="https://github.com/TheNightmanCodeth/RPS"&gt;my GitHub&lt;/a&gt;. I'll be publishing a full tutorial on YouTube soon, so keep an eye on my page here for updates if you're interested!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
