Reachability.swift

repository·master·Indexed 27 days ago

https://github.com/ashleymills/reachability.swift

A Swift replacement for Apple's original Reachability sample that monitors network connectivity changes (WiFi, Cellular, or Unavailable). It supports monitoring via closures or NotificationCenter and can be installed via Swift Package Manager, CocoaPods, Carthage, or manual integration.

Tokens
1.2K
Snippets
4
Records
6
Agent score
43%

What's inside Reachability.swift

  1. Install Reachability.swift via Swift Package Manager (SPM)

    master

    To integrate Reachability.swift using Apple's Swift Package Manager in Xcode:

    1. Go to File -> Swift Packages -> Add Package Dependency...
    2. Enter the package URL: https://github.com/ashleymills/Reachability.swift
    3. Choose the latest release.
  2. Install Reachability.swift via Carthage

    master
    1. Install Carthage via Homebrew:
      $ brew update
      $ brew install carthage
    2. Add github "ashleymills/Reachability.swift" to your Cartfile.
    3. Run carthage update.
    4. Drag Reachability.framework from Carthage/Build/iOS/ to the Linked Frameworks and Libraries section of your Xcode project's General settings.
    5. Add $(SRCROOT)/Carthage/Build/iOS/Reachability.framework to the Input Files of your Run Script Phase for Carthage.
    6. Import in code using import Reachability.
    $ brew update
    $ brew install carthage
  3. Install Reachability.swift via CocoaPods

    master

    To install Reachability.swift using CocoaPods, update your Podfile to include use_frameworks! and the ReachabilitySwift pod, then run pod install. Import the module in your code using import Reachability.

    use_frameworks!
    pod 'ReachabilitySwift'
  4. Monitor network reachability using NotificationCenter

    master

    You can observe reachability changes using standard NotificationCenter observers. Note: All notifications are delivered on the main queue.

    1. Observe the .reachabilityChanged notification.
    2. Call startNotifier() to begin.
    3. Use stopNotifier() and removeObserver to clean up.

    The notification object is the Reachability instance itself, which contains the current connection state.

    // declare this property where it won't go out of scope relative to your listener
    let reachability = try! Reachability()
    
    // Inside viewWillAppear or similar:
    NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
    
    do {
        try reachability.startNotifier()
    } catch {
        print("could not start reachability notifier")
    }
    
    @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 .unavailable:
            print("Network not reachable")
        }
    }
    
    // To stop notifications:
    reachability.stopNotifier()
    NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
  5. Monitor network reachability using closures

    master

    You can monitor network changes using the whenReachable and whenUnreachable closures. Note: All closures are executed on the main queue.

    Ensure the reachability instance is declared in a scope where it will not be deallocated while you are listening. Use startNotifier() to begin monitoring and stopNotifier() to stop.

    // declare this property where it won't go out of scope relative to your listener
    let reachability = try! 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")
    }
    
    // To stop notifications:
    reachability.stopNotifier()