In my app, I have integrated rechability.swift
. It is working fine when wifi is connected, but I want to test a scenario like when I'm connected to a mobile (hotSpot with no internet connection). When this code always executes like connected, because the status is connected with wifi, but I need to check if the network is available or not. Any other way to do as standard way ? Please help.
using rechability same result
let reachability: Reachability
do {
// reachability = try Reachability.NetworkReachable()
reachability = try Reachability.reachabilityForInternetConnection()
if (reachability.isReachableViaWiFi()) {
_label.text = "wifi connected"
}
else if (reachability.isReachable()) {
_label.text = "reachable"
}
else if (reachability.isReachableViaWWAN()) {
_label.text = "connected via wan"
}
else {
_label.text = "not connected "
}
} catch {
print("Unable to create Reachability")
return
}
Other code I used gave me the same result:
func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = flags.contains(.Reachable)
let needsConnection = flags.contains(.ConnectionRequired)
// let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
// let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}