UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

4      

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?

4      

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

5      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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

9      

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!

4      

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.

4      

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?

5      

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

5      

@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)

        }
    }

4      

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.

7      

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

4      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.