NWConnection and "Network is Down" error connecting to ip address and port

I have a pretty straightforward code to connect to a given ip address and port using NWConnection. On the first attempt to connect to the Local Network I see the "Local Network Privacy" dialog and sometimes if I press "Don't Allow" on that dialog everything starts to fail. No matter I do I keep receiving the following error:

POSIXErrorCode(rawValue: 50): Network is down

Obviously going to settings and allowing local network access to the app doesn't fix the issue. Neither deleting the app and installing it again. The only workaround I found is to restart my device which is unacceptable for a user.

My code is really simple, I tried a lot of different approaches to connect but neither approach worked.

I am on iOS 18. Is it a known bug? could be possible a bug on my code?

Any idea would be really appreciated, thanks!

could be possible a bug on my code?

That’s doesn’t seem likely. You shouldn’t have to restart your device for iOS to recognise local network privacy changes, regardless of how you wrote your code.

Are you able to test on a different device? If so, do you see the same problem there?

Does the problem reproduce with a small test app?

Is there any chance your app has a main executable UUID that’s not unique? For more on that, see the Build-time considerations section of TN3179 Understanding local network privacy.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hey @jpgarcia80 / @DTS Engineer just wanted to +1 this thread, we have recently implemented an ethernet connection and find that if the user denies Local Network permission, "everything fails" and the device must be rebooted for any changes to get recognized. We also see "Network is Down", and the issues persist through app deletions and re-installations.

We have iPhone 15's we're testing with, and we compared them to see if there's any differences - there doesn't seem to be. Same OS, same model iPhone, same app build, bug only presents on some phones and not others, seemingly by random.

Starting to wonder if this is an issue in iOS 18.X.

Hey Quinn / @DTS Engineer , question regarding the Mach-O UUID scenario here:

Let's say we have app variants for each of our environments, Dev, QA, Production.

The intent is that nobody should have more than one of these variants installed at any given time.

I just ran the dwarfdump, and found that these variants are producing the same UUID values, but is that an issue for this scenario? Your example is for when users might have a Pro and Lite version installed simultaneously, so this scenario is a bit different.

Would iOS care if different environment variants of the same app used the same UUID, even if those are only installed one at a time?

Let's say we have app variants for each of our environments, Dev, QA, Production.

Do they have the same bundle ID?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hey @DTS Engineer / @davidprivorostrauss Thank you guys for your answers. I was able to reproduce this problem on an small and basic test app. And regarding your thoughts about iOS 18 I can confirm that the exact same code works wonderfully under iOS 16 - on a different device though.

Thanks

@DTS Engineer A couple more things, the bug is reproducible in different iOS 18 devices. I have a very basic sample app which uses NWListener, NWConnection and NWBrowser. The bug is reproducible only when I try to connect to a host & port, but If connect through Bonjour the bug doesn't happen, everything works as expected. Thanks.

Note that it’s best to reply as a reply. See Quinn’s Top Ten DevForums Tips for more on that.

davidprivorostrauss wrote:

each variant has it's own Bundle ID.

That’s not great. In general, if two apps have different bundle IDs, you really want them to have different main executable UUIDs.

However, if as you say, no user has installed both apps at the same time, I don’t see how it could trigger this problem.


As to the bigger picture question, it sounds like different folks are seeing it with different protocols. So lemme start with the easier protocol, TCP.

Here’s some trivial test code:

var connectionQ: NWConnection? = nil

func start() -> NWConnection {
    print("connection will start")
    let connection = NWConnection(to: .hostPort(host: "192.168.1.39", port: 80), using: .tcp)
    connection.stateUpdateHandler = { newState in
        print("connection did change state, new: \(newState)")
    }
    connection.start(queue: .main)
    return connection
}

func stop(connection: NWConnection) {
    print("connection will stop")
    connection.stateUpdateHandler = nil
    connection.cancel()
}

func startStop() {
    if let connection = self.connectionQ {
        self.connectionQ = nil
        self.stop(connection: connection)
    } else {
        self.connectionQ = self.start()
    }
}

where 192.168.1.39 is a Mac on my local netwok that’s running an HTTP server.

I wired startStop() up to a button and ran it on an iOS 18 device. When I tap the button I get the Local Network alert. I tapped Don’t Allow and the connection eventually failed with:

connection did change state, new: waiting(POSIXErrorCode(rawValue: 50): Network is down)

I tapped the button again to stop the connection. I then navigated to Settings > Privacy & Security > Local Network and enabled access for my app. Back in the app, I tapped the button again and saw this:

connection will start
connection did change state, new: preparing
connection did change state, new: ready

So, while I’m not gonna deny there’s a problem here, the absolute basics do work.

I’m testing with Xcode 16.1 targeting iOS 18.1. My test device is on iPhone 16 with Wi-Fi but not WWAN.

Can you folks repeat the same test on your devices and let me know what you see? Or if I’ve misunderstood your test process, let me know where I went wrong.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NWConnection and "Network is Down" error connecting to ip address and port
 
 
Q