Giter VIP home page Giter VIP logo

reachability.swift's Introduction

Reachability.swift

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

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

NOTES:

  • As of Swift 1.2, you cannot convert Swift closures into C-function pointers, meaning we can't set an SCNetworkReachabilityCallBack. To get around this, this reachability replacement uses a dispatch_source firing at 1/2 second intervals.

  • If an application has the privacy option “Use cellular data” turned off, the Reachability class still reports isReachable() to be true. There is currently no (non-private) API to detect this. If you need this feature, please raise file a bug report with Apple to get this fixed. See devforums thread for details: https://devforums.apple.com/message/1059332#1059332

Installation

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', git: 'https://github.com/ashleymills/Reachability.swift'
  3. Run pod install.

Manual

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

Example - closures

let reachability = Reachability.reachabilityForInternetConnection()

reachability.whenReachable = { reachability in
    if reachability.isReachableViaWiFi() {
        println("Reachable via WiFi")
    } else {
        println("Reachable via Cellular")
    }
}
reachability.whenUnreachable = { reachability in
    println("Not reachable")
}

reachability.startNotifier()

Example - notifications

let reachability = Reachability.reachabilityForInternetConnection()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)

reachability.startNotifier()

and

func reachabilityChanged(note: NSNotification) {

    let reachability = note.object as! Reachability

    if reachability.isReachable() {
        if reachability.isReachableViaWiFi() {
            println("Reachable via WiFi")
        } else {
            println("Reachable via Cellular")
        }
    } else {
        println("Not reachable")
    }
}

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

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.