title: How to check internet connection in Swift 3 or Swift 4?
body: <p>I have tried checking internet connection in Swift 3 but the code is not working for me.</p>
  
  <pre><code> class func isConnectedToNetwork() -&gt; 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(MemoryLayout.size(ofValue: zeroAddress))
      zeroAddress.sin_family = sa_family_t(AF_INET)
  
      let defaultRouteReachability = withUnsafePointer(&amp;zeroAddress) {
        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
      }
  
      var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: UInt32(0))
      if SCNetworkReachabilityGetFlags(defaultRouteReachability, &amp;flags) == 0 {
        return false
      }
  
      let isReachable = (flags &amp; UInt32(kSCNetworkFlagsReachable)) != 0
      let needsConnection = (flags &amp; UInt32(kSCNetworkFlagsConnectionRequired)) != 0
  
      return (isReachable &amp;&amp; !needsConnection) ? true : false
    }
  </code></pre>
  
  <p>I have also imported the SystemConfiguration framework. Please suggest how to check.</p>