I instantiate the listener:
Code Block var listener: NWListener? do { listener = try NWListener(using: .udp, on: port) } catch { print("exception upon creating listener") }
I implement the stateUpdateHandler:
Code Block listener?.stateUpdateHandler = {(newState) in switch newState { case .ready: print("ready") default: break } }
And of course the handler for new connection where I also start the connection:
Code Block listener?.newConnectionHandler = {(newConnection) in newConnection.stateUpdateHandler = {newState in switch newState { case .ready: print("ready") default: break } } newConnection.start(queue: DispatchQueue(label: "newconn")) }
Finally I receive the message with
Code Block connection.receiveMessage { (data, context, isComplete, error) in // Decode and continue processing data }
The code works perfectly when the app is actively in the foreground.
What is the recommended approach to keep listening in the background when the user switches to another phone or presses the power button?
I was looking at the "Background Modes"-Capability and first thought about getting this to work with "Background processing", but after some research this does not seem suitable.
Is there any other suitable way to get the UDP listening to work in the background?
The app is also supposed to track the users location as well, also in the background. This would work with "Location updates" I suppose. Correct?
I have written a location tracker test app already and trying to get the location tracking working in the background. I need to test a little more.
In case I get location tracking in the background to work, will then the UDP listener also work in the background?
Any insights highly appreciated!