ViewController.swift
At first, you have to setup a service MultipeerConnectivityService
that provides scene synchronization among all peers in a multipeer connectivity session MCSession
.
import RealityKit
import MultipeerConnectivity
var arView = ARView(frame: .zero)
private let serviceName = "service"
override init() {
let mcPeerID = MCPeerID(displayName: UIDevice.current.name)
self.serviceAdvertiser.delegate = self
self.serviceAdvertiser.startAdvertisingPeer()
self.serviceBrowser.delegate = self
self.serviceBrowser.startBrowsingForPeers()
let session = MCSession(peer: mcPeerID, securityIdentity: nil,
encryptionPreference: .required)
arView.scene.synchronizationService =
try? MultipeerConnectivityService(session: session)
}
If you implemented ARWorldTrackinConfig in ARKit, don't forget to turn isCollaborationEnabled
instance property On that opts you in to a peer-to-peer multiuser AR experience.
import ARKit
let collabConfig = ARWorldTrackingConfiguration()
collabConfig.isCollaborationEnabled = true
arView.session.run(collabConfig)
info.plist
Secondly you have to setup NSLNUD
and NSBonjourServices
in property list file:
<key>NSLocalNetworkUsageDescription</key>
<string>LNUD</string>
<key>NSBonjourServices</key>
<array>
<string>_service._tcp</string> // Transmission Control Protocol
<string>_service._udp</string> // User Datagram Protocol
</array>