Skip to content

sauvikdolui/NetworkStatusMonitorPart2

Repository files navigation

NetworkStatusMonitorStarter

Repository for project of Medium Blog Network reachability status monitoring on iOS (Part 2)

How to use?

  1. Install ReachibilitySwift with pod ‘ReachabilitySwift’, ‘~> 3’ with cocoapods
  2. Drag and Drop ReachabilityManager.swift to your project from ...NetworkStatusMonitorPart1/NetworkStatusMonitor/Networking/Core

Start monitoring for reachability

Override AppDelegate's func applicationDidBecomeActive(_:)

func applicationDidBecomeActive(_ application: UIApplication) {
    // Starts monitoring network reachability status changes
    ReachabilityManager.shared.startMonitoring()
}

Stop monitoring for reachability

Override AppDelegate's func applicationWillEnterForeground(_:)

func applicationWillEnterForeground(_ application: UIApplication) {
  // Stops monitoring network reachability status changes
  ReachabilityManager.shared.stopMonitoring()
}

Listen from View Controllers

1. Implement `NetworkStatusListener` in `ViewController` class
extension ViewController: NetworkStatusListener {

    func networkStatusDidChange(status: Reachability.NetworkStatus) {

        switch status {
        case .notReachable:
        debugPrint("ViewController: Network became unreachable")
        case .reachableViaWiFi:
        debugPrint("ViewController: Network reachable through WiFi")
        case .reachableViaWWAN:
        debugPrint("ViewController: Network reachable through Cellular Data")
        }

        // Take necessary actions here

    }
}
2. Override `viewWillAppear(_:)` and `viewDidDisappear(_:)`
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    ReachabilityManager.shared.addListener(listener: self)
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    ReachabilityManager.shared.removeListener(listener: self)
}

Result

Output Video

About

Project for Medium Blog

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages