Implementing Reachability with Singleton


Download Reachability class

Click to download

Rechability Checker

import Foundation

public class ReachabilityChkr: NSObject {
    
    let reachability = Reachability()!
    
    override init() {
        super.init()
        reachability.whenReachable = { reachability in
            performUIUpdatesOnMain({
                if reachability.isReachableViaWiFi {
                    print("Reachable via WiFi")
                } else {
                    print("Reachable via Cellular")
                }
            })
        }

        reachability.whenUnreachable = { reachability in
            performUIUpdatesOnMain({
                print("Not reachable")
            })
        }

        do {
            try reachability.startNotifier()
        } catch {
            print("Unable to start notifier")
        }
    }

    public class var shared: Reachability {
        get {
            struct Single {
                static var shared = ReachabilityChkr()
            }
            return Single.shared.reachability
        }
    }

}

Use above class

if ReachabilityChkr.shared.isReachableViaWiFi == true || ReachabilityChkr.shared.isReachableViaWWAN == true {
    print("Connected to Internet")    
} else {
    print("=== NOT Connected to Internet ===")
}