Hello,
I'm just start learning swift and Xcode 12.0 and writing a very simple code for iPhone 6 running iOS 14.4 that sends 0 or 1 over UDP on the local network. I'm stuck at creating the NWEndpoint - the destination for my datagram. It's always invalid when debugging on the device, saying that interface is nil. I suspect that my app does not have access to the wifi interface but I can't figure out how to enable it. WiFi is on and has IP address in the same range 192.168.1.102. I read the FAQ about Local Network Privacy and can't see what do I need to do here.
let ip4 = IPv4Address("192.168.1.44")!
let host = NWEndpoint.Host.ipv4(ip4)
Thanks!
I'm just start learning swift and Xcode 12.0 and writing a very simple code for iPhone 6 running iOS 14.4 that sends 0 or 1 over UDP on the local network. I'm stuck at creating the NWEndpoint - the destination for my datagram. It's always invalid when debugging on the device, saying that interface is nil. I suspect that my app does not have access to the wifi interface but I can't figure out how to enable it. WiFi is on and has IP address in the same range 192.168.1.102. I read the FAQ about Local Network Privacy and can't see what do I need to do here.
let ip4 = IPv4Address("192.168.1.44")!
let host = NWEndpoint.Host.ipv4(ip4)
Thanks!
Thank you for looking into my problem and sorry for not submitting all the code. I used something very similar to yours and I now tried your sample with no luck.
I don't think the listener is required to receive a UDP packet but I tried running it as well. In my opinion the problem is the endpoint - it is marked as invalid in my debug session because interface is nil.
I also used a monitor
and in debug session it shows 0 available interfaces. But in the device log I see this message for my process:
Code Block var data = Data(count: 0) data.append(contentsOf: [1]) let ip4 = IPv4Address("192.168.1.44")! let host = NWEndpoint.Host.ipv4(ip4) let port = NWEndpoint.Port("4444")! let endpoint = NWEndpoint.hostPort(host: host, port: port) let udpOption = NWProtocolUDP.Options() let params = NWParameters(dtls: nil, udp: udpOption) var conn = NWConnection(to: endpoint, using: params) conn.send(content: data, completion: NWConnection.SendCompletion.contentProcessed({ NWError in NSLog("%@", "\(NWError)") }))
I don't think the listener is required to receive a UDP packet but I tried running it as well. In my opinion the problem is the endpoint - it is marked as invalid in my debug session because interface is nil.
I also used a monitor
Code Block var monitor = NWPathMonitor(requiredInterfaceType: .wifi) monitor.start(queue: .main)
and in debug session it shows 0 available interfaces. But in the device log I see this message for my process:
Code Block nw_path_evaluator_start [BFB74778-B32B-4F15-8A78-BC38C1918A0C <NULL> generic, multipath service: 1, indefinite] path: satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns