Coder Social home page Coder Social logo

hm-91 / reachability.swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ashleymills/reachability.swift

0.0 1.0 0.0 353 KB

Replacement for Apple's Reachability re-written in Swift with closures

License: MIT License

Swift 95.72% Ruby 2.61% Objective-C 1.67%

reachability.swift's Introduction

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 - 12.0), OSX (10.9 - 10.14) and tvOS (9.0 - 12.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.

Please consider sponsoring me https://github.com/sponsors/ashleymills, it's a great way to say thanks!

If you're an iOS developer who's looking for a quick and easy way to create App Store screenshots, please try out my app Screenshot Producer

Devices  Layout Copy Localize Export      

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

Thanks Ash

Got a problem?

Please read https://github.com/ashleymills/Reachability.swift/blob/master/CONTRIBUTING.md before raising an issue.

Installation

Manual

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

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. To install Reachability.swift with CocoaPods:

  1. Make sure CocoaPods is installed.

  2. Update your Podfile to include the following:

    use_frameworks!
    pod 'ReachabilitySwift'
  3. Run pod install.

  1. In your code import Reachability like so: import Reachability

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To install Reachability.swift with Carthage:

  1. Install Carthage via Homebrew
$ brew update
$ brew install carthage
  1. Add github "ashleymills/Reachability.swift" to your Cartfile.

  2. Run carthage update.

  3. Drag Reachability.framework from the Carthage/Build/iOS/ directory to the Linked Frameworks and Libraries section of your Xcode project’s General settings.

  4. Add $(SRCROOT)/Carthage/Build/iOS/Reachability.framework to Input Files of Run Script Phase for Carthage.

  5. In your code import Reachability like so: import Reachability

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 = 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")
}

and for stopping notifications

reachability.stopNotifier()

Example - notifications

NOTE: All notifications are delivered on the main queue.

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

//declare this inside of viewWillAppear

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

and

@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")
  }
}

and for stopping notifications

reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, 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

reachability.swift's People

Contributors

ashleymills avatar hamchapman avatar connorpower avatar delannoyk avatar uny avatar abunur avatar mugunthkumar avatar p4checo avatar bm-w avatar bgreenlee avatar corteggo avatar dstorm-fl avatar mokagio avatar raburski avatar rgoble4 avatar jcesarmobile avatar abizern avatar adityatrivedi avatar zummenix avatar amarcadet avatar carlhunterroach avatar damonjones avatar daniarnaout avatar dannymout avatar drichardson avatar plashchynski avatar gaudecker avatar elegyd avatar esqarrouth avatar ivanbruel avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.