How to handle a connection timing out?
let connection = NWConnection(to: endpoint, using: .tcp)
connection.stateUpdateHandler = { [self] newState in
print("newState: \(newState)")
}
connection.start(queue: .main)
Log:
newState: preparing
nw_socket_handle_socket_event [C1.1.4.1:1] Socket SO_ERROR [60: Operation timed out]
... several more times
The stateUpdateHandler function is never called again.
It turns out
NWConnectiondoesn't implement timeouts. We have to do it ourselves: