Name: Reachability.swift
Owner: Kik Interactive
Description: Replacement for Apple's Reachability re-written in Swift with closures
Forked from: ashleymills/Reachability.swift
Created: 2016-10-27 17:43:49.0
Updated: 2017-02-02 10:22:46.0
Pushed: 2016-10-27 18:20:51.0
Size: 205
Language: Swift
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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
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
'ReachabilitySwift', '~> 2.4'
To install Reachability.swift for Swift 3.x using CocoaPods, include the following in your Podfile
'ReachabilitySwift', '~> 3'
Previously:
s func reachabilityForInternetConnection() throws
Now:
?()
Previously:
(hostname: String) throws
Now:
?(hostname: String)
Previously:
ic enum NetworkStatus: CustomStringConvertible {
case NotReachable, ReachableViaWiFi, ReachableViaWWAN
Now:
ic enum NetworkStatus: CustomStringConvertible {
case notReachable, reachableViaWiFi, reachableViaWWAN
Please read https://github.com/ashleymills/Reachability.swift/wiki/Raising-an-issue before raising an issue.
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
Just drop the Reachability.swift file into your project. That's it!
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:
frameworks!
'ReachabilitySwift'
Run pod install
.
In your code import Reachability like so:
import ReachabilitySwift
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To install Reachability.swift with Carthage:
Install Carthage via Homebrew
ew update
ew install carthage
Add github "ashleymills/Reachability.swift"
to your Cartfile.
Run carthage update
.
Drag Reachability.framework
from the Carthage/Build/iOS/
directory to the Linked Frameworks and Libraries
section of your Xcode project?s General
settings.
Add $(SRCROOT)/Carthage/Build/iOS/Reachability.framework
to Input Files
of Run Script Phase for Carthage.
clare this property where it won't go out of scope relative to your listener
reachability = Reachability()!
hability.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")
}
}
hability.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")
}
try reachability.startNotifier()
tch {
print("Unable to start notifier")
and for stopping notifications
hability.stopNotifier()
This sample will use NSNotification
s 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.
clare this property where it won't go out of scope relative to your listener
reachability = Reachability()!
clare this inside of viewWillAppear
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged),name: ReachabilityChangedNotification,object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
and
reachabilityChanged(note: NSNotification) {
t reachability = note.object as! Reachability
reachability.isReachable {
if reachability.isReachableViaWiFi {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
else {
print("Network not reachable")
and for stopping notifications
hability.stopNotifier()
tificationCenter.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