I am developing an app designed for use in hospital environments where public internet access may not be available. The app consists of two parts: the main app and a Local Connectivity Extension. Both components rely on socket connections (TCP and UDP) to maintain communication with a server. We have observed consistent disconnections occurring every 1-2 hours when app in the foreground (both extension and app), and would like to clarify Apple's guidelines regarding long-term socket connections. Specifically, is it permissible to keep a continuous socket connection (both TCP and UDP) active for an extended period? If so, are there any restrictions or best practices we should follow to ensure stable connectivity? Thank you for your assistance.
I’m presuming that we’re talking iOS here. Let me know if that’s wrong.
AFAIK there’s nothing in the system that’ll prevent you from maintaining a long-term TCP connection (or UDP flow) as long your process doesn’t get suspended. If you’re doing this from an app that remains in the foreground, that app won’t get suspended and thus your networking will persist.
IMPORTANT Remember that locking the device moves your app to the background and thus may make it eligible for suspension.
There is one potential sticking point here, namely you should set UIRequiresPersistentWiFi
. See The iOS Wi-Fi Lifecycle for an explanation as to why.
My experience is that this sort of problem is commonly caused by the network infrastructure rather than iOS itself. For example, if your network uses NAT then it’s common for the NAT gateway to ‘garbage collect’ mappings after some period of inactivity. And there are lots of other middleboxen that do similar things. And I regularly see issues like this where the limit is being imposed by the Wi-Fi infrastructure itself!
In situations like this it’s important to find out where the connection is tearing. As a first step, use an RVI packet trace to see what’s happening at the TCP/IP level. That’ll tell you which side of the connection is initiating the disconnect.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"