Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created June 25, 2020 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/ab2840192d8a6941a028982d5a92af3c to your computer and use it in GitHub Desktop.
Save fitomad/ab2840192d8a6941a028982d5a92af3c to your computer and use it in GitHub Desktop.
extension CercaViewModel: MCSessionDelegate
{
...
///
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID)
{
guard peerID.displayName == self.peerName else
{
// Llegan datos de un cliente que no es
// con el que hemos iniciado la sesión
return
}
guard let discoveryToken = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NIDiscoveryToken.self, from: data) else
{
fatalError("No se ha podido leer el token del otro dispositivo")
}
// Creamos la configuración...
let config = NINearbyPeerConfiguration(peerToken: discoveryToken)
// ...arrancamos la sesión de NearbyInteraction...
self.nearbySession?.run(config)
// ...y guardo el token del cliente por si tengo
// que reanudar mi sesión.
self.peerToken = discoveryToken
DispatchQueue.main.async {
self.isConnectionLost = false
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment