暂无描述

Eeli Reilin a2c5de1861 Reformat license into standard format 10 年之前
Reachability 1bb7854300 Update version 10 年之前
Reachability.xcodeproj a0079e8c49 Fix signing issue 10 年之前
ReachabilityMac b0ce36386e Add Mac target 10 年之前
ReachabilityMacSample 5f108cbbec Add Mac sample 10 年之前
ReachabilitySample 5f108cbbec Add Mac sample 10 年之前
.gitignore 79f8d2502f Add test harness app 11 年之前
LICENSE a2c5de1861 Reformat license into standard format 10 年之前
README.md 4788551ca3 Update README.md 10 年之前
ReachabilitySwift.podspec 1ad72a9482 Update podspec 10 年之前

README.md

Reachability.swift

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

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, 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!

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.

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
    
  2. Add github "ashleymills/Reachability.swift" to your Cartfile.

  3. Run carthage update.

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

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

Example - closures

let reachability: Reachability
do {
    reachability = try Reachability.reachabilityForInternetConnection()
} catch {
    print("Unable to create Reachability")
    return
}


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.

let reachability: Reachability
do {
    reachability = try Reachability.reachabilityForInternetConnection()
} catch {
    print("Unable to create Reachability")
    return
}

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() {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    } else {
        print("Not reachable")
    }
}

and for stopping notifications

reachability.stopNotifier()
NSNotificationCenter.defaultCenter().removeObserver(self,
                                                    name: ReachabilityChangedNotification,
                                                    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