0

When I try to check for an internet connection using this method, it's stuck on infinite loop when there is WiFi but there are not actually any working connection, is there any way to add a timeout on this method.

import SystemConfiguration

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)
    }
}
Mouhammad
  • 19
  • 4
  • Possible duplicate of http://stackoverflow.com/questions/25398664/check-for-internet-connection-availability-in-swift – NSNoob Nov 02 '15 at 14:22
  • I'm using the answer of the given link, please read about my problem before making any action – Mouhammad Nov 02 '15 at 14:28
  • I did not flag your answer because you want time-out for that method which is technically different thing even though It is unlikely that such a high rated answer would be causing issues. I am just putting it out there to let mods know. . – NSNoob Nov 02 '15 at 14:34

0 Answers0