Post

Replies

Boosts

Views

Activity

NWConnection Always Fails on Physical iPhone but Works on Simulator
When I was attempting to establish a TCP connection using NWConnection on a physical iPhone, the connection consistently fails with the error: connection did change state, new: waiting(POSIXErrorCode(rawValue: 50): Network is down) connection did change state, new: preparing connection did change state, new: waiting(POSIXErrorCode(rawValue: 50): Network is down) However, when running the same code in the iOS Simulator, the connection succeeds without issues. I have already granted the app permission to access the local network by selecting "Allow [App Name] to find devices on the local network" when prompted. The issue persists even after restarting the app, device, and server. Additionally, I have configured the Info.plist file with the following keys: NSAllowsLocalNetworking NSLocalNetworkUsageDescription Here is my code: func start() -> NWConnection { let myHost: NWEndpoint.Host = "192.168.1.154" // My MacBook let myPort: NWEndpoint.Port = 1234 let connection = NWConnection(to: .hostPort(host: myHost, port: myPort), using: .tcp) connection.stateUpdateHandler = { newState in print("connection did change state, new: \(newState)") } connection.start(queue: .main) return connection } Development environment: Xcode 15.3 MacOS 14.4.1 Device OS: iOS 18.1
8
0
326
Dec ’24