Check Network Reachability in Swift 4

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 is many way 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 :

  1. Make sure CocoaPods is installed.
  2. 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
  1. Add github “ashleymills/Reachability.swift” to your Cartfile.
  2. Run carthage update.
  3. Drag Reachability.framework from the Carthage/Build/iOS/ directory to the Linked Frameworks and Librariessection of your Xcode project’s General settings.
  4. Add $(SRCROOT)/Carthage/Build/iOS/Reachability.framework to Input Files of Run Script Phase for Carthage.
  5. 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 Alamofire
class 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!

“Check Network Reachability in Swift 4” Posted first on ” Technology on Medium “
Author: Saeed Rezaei

Author: Pawan Kumar

Leave a Reply

Close Menu
%d bloggers like this:
Skip to toolbar