Just use one simple func to check the reachability to specified host.
- iOS 8.0+
- Xcode 7.0 beta
- Swift 2.0
CocoaPods 0.38 adds supports for Swift and embedded frameworks. You can install it with the following command:
$ gem install cocoapods
To integrate NetReachability into your Xcode project using CocoaPods, specify it in your Podfile
:
platform :ios, '8.0'
use_frameworks!
pod 'NetReachability'
Then, run the following command:
$ pod install
You should open the {Project}.xcworkspace instead of the {Project}.xcodeproj after you installed anything from CocoaPods.
For more information about how to use CocoaPods, I suggest this tutorial.
- define reachability variable
private lazy var reachability: NetReachability = NetReachability(hostname: "www.baidu.com")
- add notification
NSNotificationCenter.defaultCenter().addObserver(self, selector: "statusChanged", name: FFReachabilityChangedNotification, object: nil)
reachability.startNotifier()
- remove notification
reachability.stopNotifier()
NSNotificationCenter.defaultCenter().removeObserver(self, name: FFReachabilityChangedNotification, object: nil)
- observer func
func statusChanged() {
print(reachability.currentReachabilityStatus)
}
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip