I'm developing a simple Tic Tac Toe
game.
I'm trying to use MultipeerConnectivity
for connecting two devices together, here's my code:
AppDelegate
:
var mpcHandler = MPCHandler()
MPCHandler
:
class MPCHandler: NSObject {
// Variables
var peerID: MCPeerID!
var session: MCSession!
var browser: MCBrowserViewController!
var advertiser: MCAdvertiserAssistant?
// Functions
func setupPeerWithDisplayName (displayName: String) {
self.peerID = MCPeerID(displayName: displayName)
}
func setupSession() {
self.session = MCSession(peer: self.peerID, securityIdentity: nil, encryptionPreference: MCEncryptionPreference.optional)
self.session.delegate = self
}
func setupBrowser() {
self.browser = MCBrowserViewController(serviceType: "TicTacToe", session: self.session)
}
func advertiseSelf(advertise: Bool) {
if advertise {
self.advertiser = MCAdvertiserAssistant(serviceType: "TicTacToe", discoveryInfo: nil, session: self.session)
self.advertiser?.start()
}
else {
self.advertiser?.stop()
self.advertiser = nil
}
}
}
// Extensions
extension MPCHandler: MCSessionDelegate {
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
switch state {
case MCSessionState.connected:
print("Connected: \(peerID.displayName)")
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
let userInfo: [String : Any] = ["data": data, "peerID:": peerID]
DispatchQueue.main.async {
NotificationCenter.default.post(name: Notification.Name("deviceDidReceiveDataNotification"), object: nil, userInfo: userInfo)
}
}
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {
}
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
}
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
}
}
MainVC
:
var appDelegate: AppDelegate?
// Overridden Functions
override func viewDidLoad() {
super.viewDidLoad()
self.appDelegate = UIApplication.shared.delegate as? AppDelegate
self.appDelegate?.mpcHandler.setupPeerWithDisplayName(displayName: UIDevice.current.name)
self.appDelegate?.mpcHandler.setupSession()
self.appDelegate?.mpcHandler.advertiseSelf(advertise: true)
}
@IBAction func barButtonPressed(_ sender: UIBarButtonItem) {
if self.appDelegate?.mpcHandler.session != nil {
self.appDelegate?.mpcHandler.setupBrowser()
self.appDelegate?.mpcHandler.browser.delegate = self
self.present(self.appDelegate?.mpcHandler.browser ?? UIViewController(), animated: true, completion: nil)
}
}
Info.plist
:
Added Bonjour Services
with: _TicTacToe._tcp
and _TicTacToe._udp
When debugging, the devices find each other but when trying to connect, the connecting device gets stuck in Connecting
state, and the other device doesn't get the notification for approving the connection.
After a few seconds, it says that the connection was declined.
What could the issue be?