WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Day 83 - Project 25 MultipeerConnectivity

Forums > 100 Days of Swift

Hey guys, I'm just finished project 25, the MultipeerConnectivity thing. I want to let you know, that the project isn't working for me out of the box. I have to change the var mcAdvertiserAssistant: MCAdvertiserAssistant! to MCNearbyServiceAdvertiser! and to add the MCNearbyServiceAdvertiserDelegate protocoll. The rest is working fine.

Just want to let you know if you hit the same problem.

Stay sound and safe. zoran

1      

I'm having trouble with this project as well, however, my issue is that the multipeer connection times out. I start by running it on the simulator and host the session. When I run it on my connected iOS device and join session, I can see the simulator and when I try connecting, I never get prompted on the simulator to accept/decline. This must be a security/entitlement setting but I cannot figure out what needs to be added.

Anyone know how to resolve this?

1      

Has anyone managed to solve this problem? I tried @fxdlsRider's solution, but it didn't work, all connection attempts result in failure.

2      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

OK, with a few small changes in my Xcode project, I managed to make it work. The best news is, with this solution all the code presented by Paul is completely valid, so we don't need to change a single line in ViewController.swift. What we want to do is to OPT OUT of UISceneDelegate. Now I am still a beginner, so I am not aware of the long-run consequences of it, I just know that the project is working fine with the changes being done. Having this aside, let me write this down in a few steps:

  1. Go to Info.plist in your project and delete Application Scene Manifest row.
  2. Go to AppDelegate.swift and add var window: UIWindow? in the class.
  3. In the same class, delete everything below // MARK: UISceneSession Lifecycle line.

Now your simulator and the device should have no problem connecting to each other, no matter which one is the host.

I understand that by this method we lose the support of using scenes in our app, but in this particular case it shouldn't be a problem... hopefully :D

Also, I'd love to hear a comment on this by someone more experienced, so that we all learn if that solution is safe for our app.

See you tomorrow guys :D

5      

Hi all,

I have the same problem - I cannot connect with the device. I think that @MateusZ 's fix this with his approach but maybe someone found some other way to fix this? Thanks in advance for help!

1      

The solution given by @MateusZ worked for me.

The UISceneDelegate code is something new that the latest version of Xcode added to it's starting projects.

1      

Hey, guys. This worked for me with on two simulators, but two real devices or one real and another simulator can't find each other. Does anyone get the same problem?

1      

I am having the same issue as Atnon1

can connect two simulators to each other, but can't connect two real devices to each other

I'm actually using the glorious Multipeer Kit https://github.com/insidegui/MultipeerKit

which has been awesome so far until I tested this aspect

1      

@edwonedwon & @atnon1 - did you manage to solve the challange#3 ?

"Add a button that shows an alert controller listing the names of all devices currently connected to the session – use the connectedPeersproperty of your session to find that information."

not sure how to extarct displayName from mcSession.connectedPeers...

update - here is how i managed :

 @objc func namesDisplay() {
        if let userSession = mcSession?.connectedPeers.map({$0.displayName}) {
            let user = userSession.joined(separator: "-")
            let ac = UIAlertController(title: "Connected users", message: "Users = \(user))", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)

        }
    }

1      

Hi! You should look at this topic on stack overflow (sorry, Paul 😃) https://stackoverflow.com/questions/58563621/multipeer-connectivity-not-working-after-xcode-11-update - change MCAdvertiserAssistant to MCNearbyServiceAdvertiser, conform to MCNearbyServiceAdvertiserDelegate and implement didReceiveInvitationFromPeer. Last answer by ChuckZHB.

And be carefull with Bonjour services array in info.plist. It won't work without tcp and udp propeties: _hws-project25._tcp _hws-project25._udp

and Privacy - Local Network Usage Description property

After this changes it work between phisical devices and in phisical-emulator bind.

3      

shishkins linked solution worked for me using iOS 15 and Xcode 13.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.