I am rewriting my existing Objective-C code (iOS) to Swift and now am facing some issues with the Reachability
class of Apple for checking network availability... In my existing code I am using the following for achieving that.
var reachability: Reachability = Reachability.reachabilityForInternetConnection()
var internetStatus:NetworkStatus = reachability.currentReachabilityStatus()
if (internetStatus != NotReachable) {
//my web-dependent code
}
else {
//There-is-no-connection warning
}
And I am getting this error: network status is not convertible to string
at this line:
if (internetStatus != NotReachable)
Is there a method or class for getting network status?
I need these three conditions:
NotReachable: Obviously, there’s no Internet connection
ReachableViaWiFi: Wi-Fi connection
ReachableViaWWAN: 3G or 4G connection
Network Reachability is the network state of the user's device. We can use it to understand if the device is offline or online using either wifi or mobile data.
When you use iPhone with one hand in Portrait orientation, you can use Reachability to lower the top half of the screen so it's within easy reach of your thumb. Go to Settings > Accessibility > Touch, then turn on Reachability.
Answer. Ping is a network utility that is used to test if a host is reachable over a network or over the Internet by using the Internet Control Message Protocol “ICMP”. When you initiate an ICMP request will be sent from a source to a destination host.
In the viewWillAppear method, add an observer to the Notification Center, so every time the network state changes, like from Wifi goes to Cellular, this will be detected instantly and call the reachabilityChanged method. And in the viewDidDisappear method, remove the stop the notifier and remove the observer.
For network availability (works in Swift 2):
class func hasConnectivity() -> Bool {
let reachability: Reachability = Reachability.reachabilityForInternetConnection()
let networkStatus: Int = reachability.currentReachabilityStatus().rawValue
return networkStatus != 0
}
For a Wi-Fi connection:
(reachability.currentReachabilityStatus().value == ReachableViaWiFi.value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With