Created
August 17, 2016 12:32
-
-
Save Bashta/28aa7cd31ebde960625362120eceddbe to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Foundation | |
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(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