Created
September 6, 2016 07:37
-
-
Save dqhieu/d4b11521d5893be58929bcb1f61111cb to your computer and use it in GitHub Desktop.
Check reachability in ios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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