Search code examples
iosxcodeios7multipeer-connectivity

Why is Multipeer Connectivity Framework finding itself as a foreign peer as well as nonexistent foreign peers?


I have been working on an app implementing the Multipeer Connectivity (MC) framework. When running the app through Xcode for the first time on two separate phones, phoneBob and phoneJack, I initialize an MCSession for phoneBob under the displayName 'BobA' and an MCSession for phoneJack under the displayName 'JackA'. MC for phoneBob finds 'JackA' just fine and vice versa, and the two can connect with no problems.

However, After deleting the app and rerunning through Xcode, I initialize an MCSession for phoneBob under the displayName 'BobB' and an MCSession for phoneJack under the displayName 'JackB'. In addition to phoneBob finding the foreign peer 'JackB' who can be connected to successfully, it also finds the foreign peer 'JackA' in the log, even though 'JackA' cannot be connected to and was associated with a previous version of the app. This happens vice versa as well, where phoneJack finds not only the foreign peer 'BobB', but the foreign peer 'BobA' as well. Not Only this, but phoneBob will actually find ITSELF for both installations of the app, 'BobA' and 'BobB' as foreign peers, and phoneJack will find itself, 'JackA' and 'JackB' as foreign peers.

I call these foreign peers from previous installations of the app that cannot be connected to 'ghost peers'. The more times I uninstall and rerun the app through Xcode, the greater the amount of these ghost peers that are found by MC. Note that these ghost peers only appear upon the second and subsequent runs of the app following a deletion of the app. Restarting the phone and running the app gets rid of these ghost peers, but I am just utterly confused as to why MC would find ITSELF as a foreign peer as well as peers that don't actually exist. Is it possible that upon second installation of the app, MC is picking up on a ghost session from the first installation of the app that for some reason was never terminated? Is it possible for the session we initialized in the first installation to persist even after the app is deleted?


Solution

  • I believe the cause of these ghost peers in general is because multipeer is build on top of the Bonjour protocol which works by having clients register themselves with the router. When you forcibly shutdown, there is no opportunity for unregistration and these clients hang around until they time out.

    The fix for this is that the first time you create an MCPeerID you should serialize and save it. Then on subsequent runs you load and reuse the same peer, this will prevent you from ever seeing yourself as a ghost, because when you register yourself, you will now replace the old ghost version.

    There is a thread on the old Apple dev forums with sample code and a response from an Apple engineer confirming that this is the recommended approach: https://devforums.apple.com/message/1002886#1002886