NWListener in background iOS

I am developing an App for iOS/iPhone that communicates with an external hardware. The external hardware is a hotspot to which the iPhone connects and every 10 seconds this hardware sends data to the iPhone by opening a TCP connection on the local network to the indicated port.

On the iPhone side I use NWListener to retrieve the connections. When a connection is opened, I retrieve the data sent by the hardware and I close the connection.

Because I also need to have the exact position of the user I have activated the Location background mode using the CoreLocation framework.

If I put my application in the background everything works correctly. I retrieve the data every 10s as well as the changes in the user's positions.

After 15 minutes however, and it is very precise, the server (NWListener) no longer accepts connections as if there was a timeout or a limitation to having a server running in the background in an iOS application.

Can you help me? Thanks

Answered by sbordera in 821955022

Yes this is an AP with no connectivity. We use Wifi as a way of exchange data.

We discovered that there was an issue with CoreLocation that stops updating locations and the App goes into sleep.

So no network issue here in fact.

Thank you for you help !! your informations on "Extra ordinary network" was really useful !

Have you set the UIRequiresPersistentWiFi property?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi and thank you for your reply.

Yes I use it however I am not sure this is mandatory as the iOS App doesn't open a connection. It just listen to incoming connection on a dedicated port.

The external hardware does the connections every 10s.

When the issue happens, on the hardware side I have this kind of error:

tcp_input [C172:1] flags=[R.] seq=0, ack=2648339792, win=0 state=SYN_SENT rcv_nxt=0, snd_una=2648339791 nw_endpoint_flow_failed_with_error [C172 192.168.1.75:1234 in_progress channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, ipv6, dns, uses wifi)] already failing, returning

And then, if I just open the iOS app, bringing it to foreground for a second and put it back on background, the external hardware will continue to open new connections successfully .... until the next 15 minutes "timeout" .

And the same issue will happen again and again.

So on the iOS App I presume the listener is still active but need the App to bring to foreground regularly and this is not what I expect.

If you think that there is no limitation of having a NWListener on an iOS App that stays active while in background please tell me, so the solution of my issue is elsewhere.

I really don't understand why and don't find any answer.

Yes I use it however I am not sure this is mandatory as the iOS App doesn't open a connection.

That’s not what that property is about. Rather, it’s supposed to prevent the system from disassociating from Wi-Fi. I talk about this more in The iOS Wi-Fi Lifecycle, which is linked to from Extra-ordinary Networking.

I suggested it here because I suspect that’s what’s happening. AFAIK NWListener doesn’t have any sort of 15 minutes limit on its background viability, so there’s something else causing this failure. And UIRequiresPersistentWiFi is one of the few Wi-Fi ‘knobs’ available to third-party developers.

Speaking of Wi-Fi, back at the start you wrote:

The external hardware is a hotspot to which the iPhone connects

I presume by “hotspot” you mean it’s an AP that publishes a network. Does that network lead to the wider Internet? Or does this network lead only to the accessory itself?

Note When talking about Wi-Fi, I use the terms defined in Wi-Fi Fundamentals.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Yes this is an AP with no connectivity. We use Wifi as a way of exchange data.

We discovered that there was an issue with CoreLocation that stops updating locations and the App goes into sleep.

So no network issue here in fact.

Thank you for you help !! your informations on "Extra ordinary network" was really useful !

NWListener in background iOS
 
 
Q