I am trying to figure out if my user has an internet connection that actually works in his device. Based on this post: Check for internet connection with Swift I can check if my user is connected to a network, however, if he is connected, but his wifi is not working then the code still takes it as if he were connected and the program crashes. Any idea on how I can know if their internet is actually working?
public class Reachability {
class 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.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
if Reachability.isConnectedToNetwork() {
//I NEED TO KNOW INTERNET IS WORKING BEFORE I RUN THIS STATEMENTS
if let nextImage = defaults.objectForKey("next\(pID)Image") {
thumbIV.image = UIImage(data: NSData(contentsOfURL: NSURL(string: nextImage as! String)!)!)
nextVid.text = Parser().getT(defaults.objectForKey("next\(pID)Title") as! String)
}
Thanks in advance!