Skip to content

Instantly share code, notes, and snippets.

@fitomad

fitomad/ni_receive_token.swift Secret

Created Jun 25, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.