How to check internet connection in alamofire?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Alamofire provides NetworkReachabilityManager to monitor network connectivity changes in iOS apps. It reports whether the device can reach a host via WiFi, cellular, or not at all. However, reachability only checks if the network interface is available — it does not guarantee that a specific server is reachable or that the internet works.
Basic Reachability Check
Call stopListening() when you no longer need updates (e.g., in deinit):
Creating a Reachability Service
Wrap NetworkReachabilityManager in a singleton for app-wide access:
Start monitoring in AppDelegate:
Checking Before Making a Request
Monitoring for a Specific Host
Check reachability to a specific server instead of general internet:
Reacting to Network Changes in a ViewController
Alternative: NWPathMonitor (No Alamofire)
Apple's Network framework provides NWPathMonitor as a built-in alternative:
| Feature | Alamofire NetworkReachabilityManager | NWPathMonitor |
| Dependency | Requires Alamofire | Built-in (iOS 12+) |
| Host-specific | Yes | No |
| Interface type | WiFi vs Cellular | WiFi, Cellular, Wired, Loopback |
| Constrained path | No | Yes (path.isConstrained) |
Common Pitfalls
- Reachability is not connectivity:
NetworkReachabilityManagerchecks if a network interface is available, not if the internet actually works. A device can be connected to WiFi but have no internet access (captive portal, DNS failure). For true connectivity checks, make a lightweight HTTP request. - Memory leaks: Always use
[weak self]in the listener closure. Forgetting this creates a retain cycle between the manager and the view controller, preventing deallocation. - Main thread UI updates: The listener callback may fire on a background queue. Dispatch to the main queue before updating UI:
DispatchQueue.main.async { ... }. - Simulator limitations: Network reachability on the iOS Simulator may not reflect the host Mac's actual network state accurately. Test on real devices.
- Stop listening: Always call
stopListening()when the monitor is no longer needed. In view controllers, do this indeinit. In singletons, call it when the app terminates.
Summary
- Use
NetworkReachabilityManagerfrom Alamofire to monitor network status changes - Wrap it in a singleton (
NetworkMonitor) for app-wide access - Check
isReachablebefore making requests to provide immediate user feedback - Use
NWPathMonitoras a built-in alternative if you do not want the Alamofire dependency - Reachability checks network interface availability, not actual internet connectivity — consider a lightweight ping for true connectivity verification
Related reading
- How To Check Response.statusCode in sendSynchronousRequest on Swift
- How to check whether a string is a valid HTTP URL?
- How to CNAME to Amazon API Gateway Endpoint
- How to combine host network with the default network in docker-compose
- How to check internet connection in React Native application for both iOS and Android?
- How to check iOS version?
- How to configure external IP address of minikube dashboard?
- how to configure ingress to direct traffic to an https backend using https

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.