iOS Development environment
Xcode 16.4, macOS 15.6.1 (24G90) Run-time configuration: iOS 17.2+
Short Description
After having successfully established an NWConnection (either as UDP or TCP), and subsequently receiving the error code:
UDP Connection failed: 57 The operation couldn't be completed. (Network.NWError error 57 - Socket is not connected), available Interfaces: [enO]
via NWConnection.stateUpdateHandler = { (newState) in ... } while newState == .failed
the data connection does not restart by itself once cellular (RF) telephony coverage is established again.
Detailed Description
Context: my app has a continuous cellular data connection while in use. Either a UDP or a TCP connection is established depending on the user settings.
The setup data connection works fine until the data connection gets disconnected by loss of connection to a available cellular phone base station. This disconnection simply occurs in very poor UMTS or GSM cellular phone coverage. This is totally normal behavior in bad reception areas like in mountains with signal loss.
STEPS TO REPRODUCE
Pre-condition
App is running with active data connection.
Action
iPhone does loss the cellular data connection previously setup. Typically reported as network error code 57.
Observed
The programmed connection.stateUpdateHandler() is called in network connection state '.failed' (OK). The self-programmed data re-connection includes:
- a call to self.connection.cancel()
- a call to self.setupUDPConnection() or self.setupConnection() depending on the user settings to re-establish an operative data connection.
However, the iPhone's UMTS/GSM network data (re-)connection state is not properly identified/notified via NWConnection API. There's no further network state notification by means of NWConnection even though the iPhone has recovered a cellular data network.
Expected
The iPhone or any other means automatically reconnects the interrupted data connection on its own. The connection.stateUpdateHandler() is called at time of the device's networking data connection (RF) recovering, subsequently to a connection state failed with error code 57, as the RF module is continuously (independently from the app) for available telephony networks.
QUESTION
How to systematically/properly detect a cellular phone data network reconnection readiness in order to causally reinitialize the NWConnection data connection available used in app.
Relevant code extract
Setup UDP connection (or similarly setup a TCP connection)
func setupUDPConnection() {
let udp = NWProtocolUDP.Options.init()
udp.preferNoChecksum = false
let params = NWParameters.init(dtls: nil, udp: udp)
params.serviceClass = .responsiveData // service type for medium-delay tolerant, elastic and inelastic flow, bursty, and long-lived connections
connection = NWConnection(host: NWEndpoint.Host.name(AppConstant.Web.urlWebSafeSky, nil), port: NWEndpoint.Port(rawValue: AppConstant.Web.urlWebSafeSkyPort)!, using: params)
connection.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
//print("UDP Socket State: Ready")
self.receiveUDPConnection(). // data reception works fine until network loss
break
case .setup:
//print("UDP Socket State: Setup")
break
case .cancelled:
//print("UDP Socket State: Cancelled")
break
case .preparing:
//print("UDP Socket State: Preparing")
break
case .waiting(let error):
Logger.logMessage(message: "UDP Connection waiting: "+error.errorCode.description+" \(error.localizedDescription), available Interfaces: \(self.connection.currentPath!.availableInterfaces.description)", LoggerLevels.Error)
break
case .failed(let error):
Logger.logMessage(message: "UDP Connection failed: "+error.errorCode.description+" \(error.localizedDescription), available Interfaces: \(self.connection.currentPath!.availableInterfaces.description)", LoggerLevels.Error)
// data connection retry (expecting network transport layer to be available)
self.reConnectionServer()
break
default:
//print("UDP Socket State: Waiting or Failed")
break
}
self.handleStateChange()
}
connection.start(queue: queue)
}
Handling of network data connection loss
private func reConnectionServer() {
self.connection.cancel()
// Re Init Connection - Give a little time to network recovery
let delayInSec = 30.0. // expecting actually a notification for network data connection availability, instead of a time-triggered retry
self.queue.asyncAfter(deadline: .now() + delayInSec) {
switch NetworkConnectionType {
case 1:
self.setupUDPConnection() // UDP
break
case 2:
self.setupConnection() // TCP
break
default:
break
}
}
}
Does it necessarily require the use of CoreTelephony class CTTelephonyNetworkInfo or class CTCellularData to get notifications of changes to the user’s cellular service provider?