swiftsocketsbackgroundudpios-app-suspended-state

NWListener udp get suspended when click on button home device


I'm receiving on my application from server everything works fine, but when i click on button home the application stop receiving data (not locking screen tablet)

             self.udpListener = try NWListener(using: .udp, on: 51361    )
        self.udpListener?.stateUpdateHandler = { (listenerState) in
            
            switch listenerState {
            case .setup:
                print("Listener: Setup")
            case .waiting(let error):
                print("Listener: Waiting \(error)")
            case .ready:
                print("Listener: Ready and listens on port: \(self.udpListener?.port?.debugDescription ?? "-")")
            case .failed(let error):

....

       func processData(_ incomingUdpConnection :NWConnection) {
    
    incomingUdpConnection.receiveMessage(completion: {(data, context, isComplete, error) in
        
        if let data = data, !data.isEmpty {
            if let string = String(data: data, encoding: .ascii) {
                print ("messageReceiver       = \(string)")
                messageReceiver = "\(messageReceiver) \n \(string)"
            }
        }
        
        if error == nil {
            self.processData(incomingUdpConnection)
        }
    })
    
}

How can i prevent the application from suspending my listener of udp socket ?

Any help will be appreciated


Solution

  • The system(OS) will suspend your current executing logic, such as: Timer, Loop, Animation,...

    So to keep app running in background, for a long time, you have to use these methods below:

    BUT, When you use CoreLocation to access the user's location, you have to ask users for permission.