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

How can I setup a host session discoverable by other peers?

Forums > Swift

Introduction

Hello!

I am building a SwiftUI app using the MultipeerConnectivity framework where a user can host or join sessions. I am using a singleton manager for handling Multipeer Connectivity with the following details (Please inform me if there is a better framework or solution for my specific case):

Problem/Desired Result

Users can either host a session or join a session. When hosting the session, I want the session to be discoverable by other devices. However, in the MultipeerConnectivity framework, it seems that the MCNeabyServiceBrowser is used for hosting sessions while the MCNearbyServiceAdvertiser is used for advertising devices that the browser can search for and explicitly invite to a session.

Attempts

Initially, I was confused and thought the advertiser was used for advertising a session for it to be discoverable by browsers. When implementing MultipeerConnectivity into my app with this idea, however, I ran into several problems because of my misuse of the framework.

Currently, I am stuck on how to implement my idea into the app. Host sessions should be discoverable using something like an MCNearbyServiceBrowser. So instead of searching for nearby peers to invite as the host of the session, a peer that is searching for a session to join should be able to tap a "Join Session" button and have a list of all discovered sessions appear.

I tried to advertise a hosted session using MCNearbyServiceAdvertiser. Peers wanting to join a session got a list of discovered sessions from MCNearbyServiceBrowser. However, when attempting to join the session, peers must use the MCNearbyServiceBrowser.invitePeer(_:) method which causes the host of a session to instead join the session of the joining player. What I want is the reverse of that. How can I go about this?

Conclusion

If I still misunderstand how the MultipeerConnectivity framework works, please tell me. Any help in achieving my goal of having one peer serve as the host of a session that is discoverable by other peers is greatly appreciated.

Apologies in advance in case this is a duplicate issue, but many issues that seemed similar are unresolved. Please link the original, resolved issue if you find one. Also, if you need more details, feel free to ask.

2      

May be a stupid question, but have you done Project 25 of 100 Days of Swift?

2      

No, it is a good question. I have completed Project 25 of 100 Days of Swift. The structure of the app I am building right now is different since I am using SwiftUI, though. I have a manager for the Multipeer Conncectivity. I'll update my post to include this. After continuing my research, it seems that this may be because the MCNearbyServiceBrowser is active whilst the MCNearbyServiceAdvertiser is also active for the hosting peer. Are there any conflicts caused by the MCNearbyServiceAdvertiser advertising while the MCNearbyServiceBrowser browses simultaneously?

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.