NIDiscoveryToken sometimes returns Nil value

I'm using NWConnection to send my discoveryTokens. When an outgoing connection is found, I have the following code:

Code Block
let session = NISession()
     
guard let token = session.discoveryToken else {
print("discoveryToken is nil")
return
}
guard let data = try? NSKeyedArchiver.archivedData(withRootObject: token, requiringSecureCoding: true) else {
return
}
// send data ...


Why is it that the discoveryToken sometimes returns nil? Is there a remedy for this?

This has happened on multiple occasions and because of this nothing is sent. There doesn't seem to be a workaround nor any documentation on why this occurs

Replies

On my iPhone:

Settings > General > Reset > Reset All Settings

Worked for me.

Cheers!
Rahul
This discoveryToken property on the NISession object can be nil if the NISession is immediately invalidated. Please check if the session:didInvalidateWithError: method on the NISessionDelegate is called immediately after creating the NISession object, the error code will indicate the issue.