|
|
10 years ago | |
|---|---|---|
| Reachability Sample | 10 years ago | |
| Reachability Test | 10 years ago | |
| .gitignore | 11 years ago | |
| LICENSE | 11 years ago | |
| README.md | 10 years ago | |
| Reachability.swift | 10 years ago | |
| ReachabilitySwift.podspec | 10 years ago |
Replacement for Apple's Reachability re-written in Swift with closures
Inspired by https://github.com/tonymillion/Reachability
NOTES:
CocoaPods is a dependency manager for Cocoa projects. To install Reachability.swift with CocoaPods:
Make sure CocoaPods is installed.
Update your Podfile to include the following:
use_frameworks!
pod 'ReachabilitySwift', git: 'https://github.com/ashleymills/Reachability.swift'
Run pod install.
Just drop the Reachability.swift file into your project. That's it!
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()
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)
Got a bug fix, or a new feature? Create a pull request and go for it!
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