Getting Started with Multipeer Connectivity | raywenderlich.com

In this tutorial, you’ll learn how to transfer data between devices with no external network. You’ll also try your hand at creating a chat feature.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/12689804-getting-started-with-multipeer-connectivity

Dear god, please do not use Multipeer Connectivity.

It fails miserably over 4 device connections including ghost connections, mysteriously dropped clients, clients not appearing when connected, clients not being visible when on the same network.

Look what people face with this terrible framework.

https://developer.apple.com/forums/thread/666810?login=true&page=1#650519022

Even Apple’s own sample code will fail with the issues I mentioned above. The framework is a time wasting disaster that should never be used.

MultipeerGroupChat,

Thanks for the info. I didn’t really have that many physical devices I could test with while writing this, so it wasn’t something I experienced. But great to let others know in case they wonder if they’re doing something wrong.

Hey there Azav, what would you suggest as an alternative to support 4+ devices in a internet/service denied environment (like out camping)?

That’s the problem. I don’t have an answer. All I have is experience that >4 devices starts causing unsolvable problems when using MultipeerConnectivity. I tested this on 10 iPads that I purchased for testing and used Apple’s sample source from Multipeer Group Chat sample source which you can get and test from here.
MultipeerGroupChat.

There have been discussions recently on Reddit in The Swift Programming Language in the past few weeks about using another framework. I can’t confirm any success or problems with it though.

Dear Macandyp,
Thank you very much for the interesting tutorial. I tested it on 2 devices and I realised that I can’t share more than one task on the receiving device. Basically I share the first task and when create a second and try to share it, it doesn’t work. I have reviewed the code by I couldn’t find the issue. Can you please tell me what might be happening and do you also observe the same issue when you run it on your device?
Greetings,
Dakata

Hey @dakata,

Sorry, just want to clarify: you’re saying that you do the following:

  • Share an item from device A to device B
  • Device B receives the item
  • Attempt to share from device A to device B
  • Device B never receives the item

Is this the correct issue?

Hey Macandyp,

Yes you are right. I use 2 physical devices to test it, both are iPhone XS.
First I create an item on phone A and share it to phone B.
Everything is successful and phone B receives it.
Then I create a second item on phone A and try to send to to phone B, in the same way like the first, but this is no longer possible. The app doesn’t crash, just phone A doesn’t want to send the second item or phoneB doesn’t accept it or maybe it is accepted but not shown on the screen. I can’t find the issue.

Thanks for the tutorial.

btw there is an issue in iOS 14. Just adding the usage permission blurb in the Info.plist isn’t enough. I got the following error when I turned on advertising.

[MCNearbyServiceAdvertiser] Server did not publish: errorDict [{ NSNetServicesErrorCode = “-72000”;
NSNetServicesErrorDomain = 10;}].

To fix this, you need to add the two services to the Info.plist as well.

<key>NSLocalNetworkUsageDescription</key>
<string>Job Manager needs to use your phone&apos;s data to discover devices nearby</string>
<key>NSBonjourServices</key>
<array>
	<string>_jobmanager-chat._tcp</string>
	<string>_jobmanager-jobs._tcp</string>
</array>

Source 1
Source 2

3 Likes

I encountered the same error until I added NSBonjourServices to Info.plist. Thanks!

Hi @macandyp, first let me say this is a well written tutorial. Like the others I can confirm iOS 14 does require the Bonjour entries to info.plist or you will just get console errors when you turn on “receive jobs”. Once you resolve that I can transfer 1 job but no more. When you try the 2nd job you will get the alert on the receiving device asking “would you like to accept”. But when you tap YES nothing happens and there is no message on the console.

@dakata @mchartier I had the same issue of not being able to send more than 1 job from device A to device B. I solved it by adding session.disconnect() to the end of session(_:didReceive:fromPeer:). Leaving the session connected caused issues on subsequent sends. Hope this will help others who study this excellent tutorial.

func session(
  _ session: MCSession,
  didReceive data: Data,
  fromPeer peerID: MCPeerID
) {
   guard let job = try? JSONDecoder()
      .decode(JobModel.self, from: data) else { return }
   DispatchQueue.main.async {
       self.jobReceivedHandler?(job)
   }
   session.disconnect()
}