This is my code:
self.reachability = Reachability.reachabilityForInternetConnection()
self.reachability.reachableBlock = {
(let reach: Reachability!) -> Void in
dispatch_async(dispatch_get_main_queue()) {
print(true)
}
}
self.reachability.unreachableBlock = {
(let reach: Reachability!) -> Void in
dispatch_async(dispatch_get_main_queue()) {
print(false)
}
}
self.reachability.startNotifier()
I called this in viewDidAppear
, but nothing happened. However, when I turned iPhone to flight mode, false
was printed immediately. And when I switched off flight mode, true
was also printed.
What I need is to check the network availability instead of change. Where is the problem?
The blocks are run when the reachability status changes, if you need to check the current reachability status and not wait for a change then you can do:
if self.reachability.isReachable() {
// reachable
} else {
// not reachable
}