Check Network Reachability in Swift 4
Network Reachability is the network state of the user’s device. We can use it to understand if the device is offline or online using either wifi or mobile data.
There are many ways to Check Network Reachability in Swift and many framework exist.
I want to share more popular and useful way to check network Reachability in swift here. if some good framework or method miss. please let me know it
Reachability.swift
This is a simple framework to check Reachability,
support CocoaPods, Carthage and manual installation.
CocoaPods :
- Make sure CocoaPods is installed.
- Update your Podfile to include the following:
use_frameworks!
pod 'ReachabilitySwift'
3. Run pod install
4. In your code import Reachability like so: import Reachability
Carthage :
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To install Reachability.swift with Carthage:
Install Carthage via Homebrew
$ brew update
$ brew install carthage
- Add
github "ashleymills/Reachability.swift"
to your Cartfile. - Run
carthage update
. - Drag
Reachability.framework
from theCarthage/Build/iOS/
directory to theLinked Frameworks and Libraries
section of your Xcode project’sGeneral
settings. - Add
$(SRCROOT)/Carthage/Build/iOS/Reachability.framework
toInput Files
of Run Script Phase for Carthage. - In your code import Reachability like so:
import Reachability
How to use
Reachability.swift Use closures and notifications to deliver Network State
sample closures
//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!
reachability.whenReachable = { reachability in
if reachability.connection == .wifi {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
}
reachability.whenUnreachable = { _ in
print("Not reachable")
}
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
and for stopping notifications
reachability.stopNotifier()
NOTE: All closures are run on the main queue.
sample notification
//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!
//declare this inside of viewWillAppear
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
and
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi")
case .cellular:
print("Reachable via Cellular")
case .none:
print("Network not reachable")
}
}
and for stopping notifications
reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
NOTE: All notifications are delivered on the main queue.
Alamofire NetworkReachabilityManager
You can create custom class like NetworkState.swift and check Alomafire NetworkReachabilityManager class to get network status
Sample
import Foundation
import Alamofireclass NetworkState {
class func isConnected() ->Bool {
return NetworkReachabilityManager()!.isReachable
}
}
Usage:
if NetworkState.isConnected() {
print("Internet is available.")
// ...
}
SCNetworkReachability
we can use the interface of SCNetworkReachability
provided by the framework SystemConfiguration
. We can use it to read the network informations both synchronously and asynchronously.
To use this interface you can check this article Network Reachability With Swift or use this gist on github provided by me and test successfully in swift 4.
If you enjoy reading this post, please share it so others can find it
Thanks!