Using Apple's Reachability to check remote server reachability in Swift
The Reachability class you're using is based on Apple's SCNetworkReachability class, which doesn't do exactly what you're hoping. From the SCNetworkReachability documentation:
A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host.
So it's not built for testing whether or not the remote host is actually online, just whether (1) the current network settings will allow an attempt to reach it and (2) by what methods. Once you've determined that the network is active you'll need to make an attempt to connect to see if the remote host is actually up and running.
Note: This test:
(ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)
is the correct way to check -- for some reason NetworkStatus
is one of the few Apple enumerations created without the NS_ENUM macro.
In swift,
About what I understood of reachability utility propose by Apple (https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html) or tonymillion (https://github.com/tonymillion/Reachability) which are basically the same is :
you have 3 possible test :
- local (can access local network but not internet)
- extern (can access internet by ip address)
- dns (can access internet and reach an hostname)
You can test them respectively by starting a notifier with this :
let wifiReachability = Reachability. reachabilityForLocalWiFi()
wifiReachability.startNotifier()
let internetReachability = Reachability.reachabilityForInternetConnection()
hostReachability.startNotifier()
let hostReachability = Reachability(hostName:"www.apple.com")
hostReachability.startNotifier()
Which will trigger a notification that you can catch with this method : NSNotificationCenter.defaultCenter().addObserver()
So to use them you can do something like that :
create a function in your appDelegate which will instantiate the notifier :
func startReachabilityTest()
{
// Allocate a reachability object to test internet access by hostname
let reach = Reachability(hostName: "www.apple.com")
// Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
//reach.reachableOnWWAN = false
reach.startNotifier()
}
Then you can call it in the didFinishLaunchingWithOptions of your appDelegate :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.startReachabilityTest();
}
if want to catch the event in any viewController just add this line to viewDidLoad :
override func viewDidLoad()
{
// Here we set up a NSNotification observer. The Reachability that caused the notification
// is passed in the object parameter
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "reachabilityChanged:",
name: kReachabilityChangedNotification,
object: nil)
}
and add this method method to react to the event :
func reachabilityChanged(notice: NSNotification)
{
println("reachability changed")
let reach = notice.object as? Reachability
if let remoteHostStatus = reach?.currentReachabilityStatus()
{
if remoteHostStatus == NetworkStatus.NotReachable
{
println("not reachable")
}
else
{
println("reachable")
}
}
}
You can also catch the event inside your appDelegate by adding NSNotificationCenter.defaultCenter().addObserver() inside didFinishLaunchingWithOptions and then add reachabilityChanged(notice: NSNotification) in it.
Ah, also note that you can easily add reachability class to your project with cocoapods by adding this line :
pod 'Reachability', '~> 3.2'
To your pod file and them after a pod install in command line just add this line to xxx-Bridging-Header.h header file (where xxx is the name of your app) :
#import <Reachability/Reachability.h>
If you don't have bridging header in your project you can follow this tutorial : http://www.learnswiftonline.com/getting-started/adding-swift-bridging-header/
No need to add systemConfiguration.framework which is already added by pod dependencies.
Note : Reachability does not work fine in the simulator
hope this help!
If you're looking for a Swift implementation of Apple's Reachability class, you could take a look at this:
http://github.com/ashleymills/Reachability.swift
It's a drop in class, using notifications and closures.
It works with iOS and OS X and has Cocoapod / Carthage support.
Good luck!