You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

6 years ago
  1. //
  2. // Reachability.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 12/6/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import SystemConfiguration
  10. public class Reachability {
  11. class func isConnectedToNetwork()->Bool{
  12. var zeroAddress = sockaddr_in()
  13. zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
  14. zeroAddress.sin_family = sa_family_t(AF_INET)
  15. let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
  16. $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
  17. SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
  18. }
  19. }
  20. var flags = SCNetworkReachabilityFlags()
  21. if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
  22. return false
  23. }
  24. let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
  25. let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
  26. return (isReachable && !needsConnection)
  27. }
  28. }