2020-02-01-Swift-Reachability

struct SomeStructure {
  func someFunction() {
    // if internet available proceed else return
    guard InternetChecker.shared.internetIsAvailable else { return }
    // do something when internet is available.
  }

Source files are as follows.

InternetChecker.swift

public enum InternetError: Error {
  case noInternet
}

public struct InternetChecker {
  private init() {}

  public static var shared = InternetChecker()

  public var internetIsAvailable: Bool {
    let status = Reach().connectionStatus()
    switch status {
      case .unknown, .offline:
        return false
      case .online(.wwan):
        return true
      case .online(.wiFi):
        return true
    }
  }
}

Reach.swift

Last updated

Was this helpful?