Keep NetService connection alive when device is locked

I'm using NetService to discover and then connect their streams to transfer data between devices. But the connection terminates when the device is locked. I've looked around but haven't found any working solutions. Is there any way to keep NetSerivce(tcp connection) alive even when the device is locked ?

Replies

The basic rules of the road here are covered by Technote 2277 Networking and Multitasking. In short, doing this sort of networking in the background is fine as long as you can prevent your app from being suspended.

If you only need to do this for a short time after moving to the background, a

UIApplication
background task is ideal. See my UIApplication Background Task Notes post for hints and tips on that API.

If you need to do it for longer, things get more complex. iOS puts strict limits on background execution, and those limits are extremely strict for unbounded background execution. Those limits are tied to user-level functionality. So, for example, if you’re an audio playing app then you can use the

audio
background mode to continue audio playback in the background, which prevents your app from being suspended, which means your networking will work.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

From what I've understood, UIApplication's background task isn't reliable as it can expire at any time. But I need a more reliable solution. I've seen apps that run very long tasks without any interuption even if the device is locked. However in my scenario, if the app goes in background by pressing the home button, the connection is not terminated but suspended and the data transfer resumes once the app moves back to foreground. My problem is with screen-lock, how can I keep the connection alive (even if the data transfer is suspended like pressing home button case) ? I just need it to remain connected.

As explained in TN2277 your app is suspended while it’s in the background, your network connections will either just stall or be closed by socket resource reclaim. So your question is equivalent to “How do I ensure my app runs indefinitely in the background?”

There isn’t a general solution to that question. It is possible for an app to run indefinitely in the background, but the mechanisms to do that are task specific, must be visible to the user, and tightly controlled by App Review.

If you explain what your app is doing with these streams, I can offer an opinion as to whether any of these mechanism are suitable for you [1].

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] I should stress that this is only an opinion. App Review has the final say about what will or won’t be allowed on the store.

I'm using the streams to transfer data between two devices and I would like the connection to stay alive at least until the transfer is complete.

In the absence of any multitasking superpowers (to use a term borrowed from TN2277),

UIApplication
background task is your only option here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"