ReachabilitySwift 4.1.0

ReachabilitySwift 4.1.0

TestsTested βœ—
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2017
SwiftSwift Version 4
SPMSupports SPM βœ—

Maintained by Ashley Mills.


Downloads

Total6,644,997
Week164,660
Month656,362

Installs

Apps60,726
Apps WeekApps This Week 6,417
Pod Tries38
Test Targets4,920
Test WeekTests This week 878
Watch Apps8
powered by Segment

GitHub

Stars4,248
Watchers117
Forks496
Issues37
Contributors44
Pull Requests5

Code

Files1
LOCLines of Code 222


  • By
  • Ashley Mills

Reachability.swift

Reachability.swift is a replacement for Apple's Reachability sample, re-written in Swift with closures.

It is compatible with iOS (8.0 - 11.0), OSX (10.9 - 10.13) and tvOS (9.0 - 11.0)

Inspired by https://github.com/tonymillion/Reachability

Supporting Reachability.swift

Keeping Reachability.swift up-to-date is a time consuming task. Making updates, reviewing pull requests, responding to issues and answering emails all take time. If you'd like to help keep me motivated, please download my free app, Photo Flipper from the App Store. (To really motivate me, pay $1.99 for the IAP πŸ˜€)

And don't forget to β˜… the repo. This increases its visibility and encourages others to contribute.

Thanks
Ash

IMPORTANT

Version 4.0 breaking changes

Previously:

enum NetworkStatus {
    case notReachable, reachableViaWiFi, reachableViaWWAN
}
var currentReachabilityStatus: NetworkStatus

Now:

enum Connection {
    case none, wifi, cellular
}
var connection: Connection

Other changes:

  • isReachableViaWWAN has been renamed to isReachableViaCellular

  • reachableOnWWAN has been renamed to allowsCellularConnection

  • reachability.currentReachabilityString has been deprecated. Use "\(reachability.connection)" instead.

  • isReachable has been deprecated. Use connection != .none instead.

  • isReachableViaWWAN has been deprecated. Use connection == .cellular instead.

  • The notification for reachability changes has been renamed from ReachabilityChangedNotification to Notification.Name.reachabilityChanged

  • All closure callbacks and notification are fired on the main queue (including when startNotifier() is called)

Got a problem?

Please read https://github.com/ashleymills/Reachability.swift/wiki/Raising-an-issue before raising an issue.

Installation

Manual

Just drop the Reachability.swift file into your project. That's it!

Example - closures

NOTE: All closures are run on the main queue.

//declare this property where it won't go out of scope relative to your listener
let reachability = 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")
}

and for stopping notifications

reachability.stopNotifier()

Example - notifications

NOTE: All notifications are delviered on the main queue.

//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!

//declare this inside of viewWillAppear

     NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(_:)), name: .reachabilityChanged, object: reachability)
    do{
      try reachability.startNotifier()
    }catch{
      print("could not start reachability notifier")
    }

and

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 .none:
    print("Network not reachable")
  }
}

and for stopping notifications

reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .eachabilityChanged, object: reachability)

Want to help?

Got a bug fix, or a new feature? Create a pull request and go for it!

Let me know!

If you use Reachability.swift, please let me know about your app and I'll put a link here… and tell your friends!

Cheers,
Ash