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.