Skip to content

Instantly share code, notes, and snippets.

@dqhieu
Created September 6, 2016 07:37
Show Gist options
  • Save dqhieu/d4b11521d5893be58929bcb1f61111cb to your computer and use it in GitHub Desktop.
Save dqhieu/d4b11521d5893be58929bcb1f61111cb to your computer and use it in GitHub Desktop.
Check reachability in ios
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
let isReachable = flags == .Reachable
let needsConnection = flags == .ConnectionRequired
return isReachable && !needsConnection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment