Check for internet connection with Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking for an internet connection in Swift sounds simple, but there are really two different questions. One is whether the device currently has a usable network path. The other is whether your actual backend or website is reachable. NWPathMonitor answers the first question well; a real request answers the second.
Use NWPathMonitor for Reachability State
Apple's modern reachability tool is NWPathMonitor from the Network framework. It tells you whether the system currently sees a satisfied path and whether that path uses Wi-Fi, cellular, or another interface.
That is the right building block for updating UI state, disabling network-dependent actions, or explaining to the user that the device currently has no route to the network.
Reachability Is Not the Same as Real Internet Access
A satisfied path does not guarantee your app can talk to the service it needs. The device may be on a captive portal, a misconfigured Wi-Fi network, or a filtered corporate network.
If your real question is "can I reach my API right now," make a small request to a health endpoint:
That second check is what proves your service is reachable, not just that some network path exists.
A Good App Usually Uses Both
In practice, many apps combine both techniques:
- '
NWPathMonitorupdates general connectivity state' - a real request confirms service availability when needed
That separation keeps the app honest. It prevents you from showing a misleading "online" badge when the device has a path but your backend is down.
Avoid the Old Synchronous Habit
Older iOS code often tried to do a one-off synchronous reachability check before every request. That pattern leads to brittle logic. A better approach is:
- observe path changes continuously
- keep networking code resilient to request failures
- handle real request errors even when reachability says a path exists
The network can always change between the check and the request anyway, so the request path still needs proper error handling.
Common Pitfalls
- Treating
NWPathMonitoras proof that your API is reachable. - Running a one-time connectivity check and assuming it stays valid.
- Forgetting to keep a strong reference to the monitor so it continues running.
- Updating UI directly from the monitor's background queue instead of dispatching to the main queue when needed.
- Building logic that skips real error handling because a reachability check passed earlier.
Summary
- Use
NWPathMonitorto learn whether the device currently has a usable network path. - Use a real
URLSessionrequest to confirm that your backend is reachable. - Reachability and service availability are related but different checks.
- Keep the monitor alive and handle updates on the correct queue.
- Even with reachability monitoring, actual requests still need robust error handling.

