Can an iOS app be a TCP socket server

Hi, I would like to know if Apple permit to connect an electronic device to an iPhone Hotspot and open a TCP socket connection between them (between the electronic device and the iPhone, and more precisely, an iOS app on the iPhone). The iPhone (app) would be the Server and the device would be the client. This will allow data transfer between them.

If it is permitted, I've read on different forums that the iPhone IP is not static and that it could make a problem to a TCP connection, is that correct? If it is, do you know a solution to that problem?

I've already tried the oposite, i.e. the iOS app being the client and the electronic device being the server, but the socket connection wasn't stable and I couldn't avoid it to close suddenly (maybe because the iOS app has to use Cellular data in parallelel in order to make requests to an external server and receive push notification from Firebase).

Thank you in advance!

In theory, sure. Some apps provide servers to transfer data. But this is only practical for a short time. It won't work for long term connections.

"But this is only practical for a short time. It won't work for long term connections."

What do you mean for a short time?

I've already understood that the socket will be closed immediatelly when the app passes to background, maybe it's what you wanted to say? For that part, I will take care not to pass the app to background when the socket is open and I want to manually set in the iPhone Settings so that the screen will never auto-lock. May this be sufficient?

I said what I wanted to say. What you describe is one part of it. Will that be sufficient? No clue. What you describe is contrary to the design goals of the device, which is intended to be a consumer of data rather than a provider. There are more advanced techniques you could use when running in the background, but that would require an equivalently advanced client on the other end.

Using the Hotspot feature on the iPhone to act as a server is not a practice I recommended. This is indicated by the lack of APIs available for this use case. Other pitfalls would include reliably on using an IP and maintaining a connection to iPhone from the client device.


There are a few recommended solutions for this situation; first, if both devices are on the same local network you could look at using peer-to-peer browsing for transferring data between both devices. NWBrowser, NWListener, and NWConnection would great in for peer-to-peer and you can take a look at this sample for insight on how to do that. The second solution would be for the device to create it's own temporary network and have the iPhone connect to this network to read data from the device. Look at using NEHotspotConfiguration for setting up a connection from the iPhone to the device.


For both solutions mentioned above I would recommend using NWConnection for TCP and UDP flows as a replacement for using sockets. Network framework is a modern alternative to sockets and should be used anywhere that you want to use sockets today in iOS.

| but the socket connection wasn't stable and I couldn't avoid it to close suddenly


Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

Can an iOS app be a TCP socket server
 
 
Q