Prevent WebSocket from closing when app goes into background momentarily

I'm creating an app that is streaming video data from the phone to a server. Specifically, I'm grabbing frames from the phone's screen and am sending those. There's a moment when I have to show a system dialog. When I do, though the app remains visible, the app's events indicate it is being put into the background. The moment it does this my various connections (including a WebSocketTask) are terminated. I attempted to configure the WebSocketTask with a URL session that could work in the background. But upon instantiation I get the error:

WebSocket tasks are not supported in background sessions

Is there something I can do to keep the WebSocket connection alive? If it could survive in the background for 30 seconds, that would be enough.

Everything I'm reading suggest that connections own by the application cannot be persisted in the background. I see a number of background scenarios such as downloading a file that are not applicable to my use case.

The key factor here is not foreground/background but running/suspended. If your app is running, the system won’t nix its network connections, it’s just that the iOS will usually suspend the app shortly after moving it to the background.

Is there something I can do to keep the WebSocket connection alive? If it could survive in the background for 30 seconds, that would be enough.

That sounds like a job for a UIApplication background task. See my UIApplication Background Task Notes post for more… background (-:

Share and Enjoy

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

Prevent WebSocket from closing when app goes into background momentarily
 
 
Q