Low-Level Networking on watchOS 7

This is the only thing I found re: low-level networking on WatchOS: developer.apple.com/forums/thread/127232
Is this still the case? Both NWListener and NWConnection are working in Simulator but not on physical device. Is this expected?

The below class works in the simulator but on physical device creating and starting the listener doesn't return anything in the stateUpdateHandler

Code Block
class Connect: ObservableObject {
    static let sharedInstance = Connect()
    private var talking: NWConnection?
    private var listening: NWListener?
    @Published var incomingUDPMessage: String = ""
    func listenUDP(port: NWEndpoint.Port) {
        print("IP Address:")
        print(getIpAddress())
        do {
            self.listening = try NWListener(using: .tcp, on: port)
            print("listening params")
            print(self.listening?.service)
            self.listening?.stateUpdateHandler = {(newState) in
                switch newState {
                case .ready:
                print("ready to listen")
                default:
                break
                }
            }
            self.listening?.newConnectionHandler = {(newConnection) in
                newConnection.stateUpdateHandler = {newState in
                    switch newState {
                    case .ready:
                    print("new connection")
                    self.receive(on: newConnection)
                    default:
                    break
                    }
                }
                newConnection.start(queue: DispatchQueue(label: "new client"))
                }
            } catch {
        print("unable to create listener")
        }
        self.listening?.start(queue: .main)
    }// END OF FUNC - LISTEN TO UDP
    func receive(on connection: NWConnection) {
        connection.receiveMessage { (data, context, isComplete, error) in
            if let error = error {
  print(error)
                return
            }
            if let data = data, !data.isEmpty {
                let incomingString = String(decoding: data, as: UTF8.self)
                    print("Incoming String")
      print(incomingString)
                    self.listening?.cancel()
            }
        }
    }// END OF FUNC - RECEIVE
}// END OF CLASS - CONNECT

Replies

Is this still the case?

Matt’s Low-Level Networking on watchOS post is still an accurate description of this situation.

Both NWListener and NWConnection are working in Simulator but not on
physical device. Is this expected?

Yes. The simulator uses the Mac’s networking stack and that doesn’t enforce this restriction. You should feel free to file a bug against the simulator but, just to set expectations, this is not easy to fix.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
  • Is there any change to this with watchOS 8?

Add a Comment