ReachabilitySwift 4.0-beta2

ReachabilitySwift 4.0-beta2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2017
SwiftSwift Version 3
SPMSupports SPM

Maintained by Ashley Mills.


Downloads

Total5,438,569
Week160,056
Month563,634

Installs

Apps52,666
Apps WeekApps This Week 7,030
Pod Tries38
Test Targets4,479
Test WeekTests This week 910
Watch Apps8
Watch Apps WeekWatch Apps This week 1
powered by Segment

GitHub

Stars4,211
Watchers117
Forks493
Issues37
Contributors44
Pull Requests2

Code

Files1
LOCLines of Code 196


  • 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 - 10.0), OSX (10.9 - 10.12) and tvOS (9.0 - 10.0)

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

IMPORTANT

Supporting Swift 2.3 and Swift 3

The source has been updated to support both Swift 2.3 (tag v2.4) and Swift 3 (tag v3.0)

To install Reachability.swift for Swift 2.3 using CocoaPods, include the following in your Podfile

pod 'ReachabilitySwift', '~> 2.4'

To install Reachability.swift for Swift 3.x using CocoaPods, include the following in your Podfile

pod 'ReachabilitySwift', '~> 3'

Swift 3 / Breaking changes

The following iOS 10 branch contains the following breaking changes:

Previously:

class func reachabilityForInternetConnection() throws

Now:

init?()

Previously:

init(hostname: String) throws

Now:

init?(hostname: String)

Previously:

public enum NetworkStatus: CustomStringConvertible {
    case NotReachable, ReachableViaWiFi, ReachableViaWWAN
}

Now:

public enum NetworkStatus: CustomStringConvertible {
    case notReachable, reachableViaWiFi, reachableViaWWAN
}

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, Foto Flipper from the App Store. (To really motivate me, pay $0.99 for the IAP!)

And don’t forget to the repo. This increases its visibility and encourages others to contribute.

Thanks Ash

Installation

Manual

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

Example - closures

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

reachability.whenReachable = { reachability in
    // this is called on a background thread, but UI updates must
    // be on the main thread, like this:
    dispatch_async(dispatch_get_main_queue()) {
        if reachability.isReachableViaWiFi() {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
}
reachability.whenUnreachable = { reachability in
    // this is called on a background thread, but UI updates must
    // be on the main thread, like this:
    dispatch_async(dispatch_get_main_queue()) {
        print("Not reachable")
    }
}

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

and for stopping notifications

reachability.stopNotifier()

Example - notifications

This sample will use NSNotifications to notify when the interface has changed. They will be delivered on the MAIN THREAD, so you can do UI updates from within the function.

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

//declare this inside of viewWillAppear

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:",name: ReachabilityChangedNotification,object: reachability)
    do{
      try reachability.startNotifier()
    }catch{
      print("could not start reachability notifier")
    }

and

func reachabilityChanged(note: NSNotification) {

  let reachability = note.object as! Reachability

  if reachability.isReachable() {
    if reachability.isReachableViaWiFi() {
      print("Reachable via WiFi")
    } else {
      print("Reachable via Cellular")
    }
  } else {
    print("Network not reachable")
  }
}

and for stopping notifications

reachability.stopNotifier()
NSNotificationCenter.defaultCenter().removeObserver(self,
                                                    name: ReachabilityChangedNotification,
                                                    object: reachability)

Got a problem?

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

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