Awesome Open Source
Awesome Open Source

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



Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages
Top Projects

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Swift (65,929
Blog (28,430
Monitoring (11,819
Drag (9,905
Medium (3,884
Reachability (411