Ola

12.0.0

Check reachability of host
michaelnisi/ola

What's New

📦 Ola 12

2020-11-22T09:55:55Z

Convert to Swift package.

Ola

The Ola Swift package lets you check network reachability of a named host. You can set a callback to run when the reachability of the host has changed. Ola is a simple Swift wrapper around some of Apple’s System Configuration APIs, making them easier to use.

Example

import Foundation
import os.log

let host = "1.1.1.1"
var probe = Ola(host: host, log: .default)

probe?.activate { status in
  print("host status: (\(host), \(String(describing: status)))")
}

sleep(10)

probe?.invalidate()
probe = nil

print("OK")

Types

enum OlaStatus: Int

OlaStatus eumerates three boiled down host states, derived from SCNetworkReachabilityFlags.

  • unknown
  • reachable
  • cellular
class Ola: Reaching

Ola is the main object of this module, it implements the tiny Reaching API:

protocol Reaching {
  func reach() -> OlaStatus
  func reach(statusBlock: @escaping (OlaStatus) -> Void)
  func activate(installing callback: @escaping (OlaStatus) -> Void) -> Bool
  func invalidate()
}

Exports

Creating a Probe

Each Ola object is dedicated to monitoring a specific host.

init?(host: String, log: OSLog?)
  • host The name of the host to monitor.

Checking Host Reachability

The common use case is to synchronously—not on the main thread though QA1693—check if a given host is reachable.

func reach() -> OlaStatus

Returns the reachability of the host: unknown, reachable, or cellular.

func reach(statusBlock: @escaping (OlaStatus) -> Void)

Same as reach(), but non-blocking, executing on a system-provided global concurrent dispatch queues.

Monitoring Host

A less common use case is getting notified, when the state of a given host has changed. For example, to reason if it’s appropiate to issue a request.

func activate(installing callback: @escaping (OlaStatus) -> Void) -> Bool

Returns true if installing the callback has been successful.

func invalidate()

Invalidates the probe removing the callback.

Install

📦 Add https://github.com/michaelnisi/ola to your package manifest.

License

MIT

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Nov 09 2022 05:40:04 GMT-0500 (GMT-05:00)