Reachability.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. Copyright (c) 2014, Ashley Mills
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  15. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  16. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  17. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  19. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  21. POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. import SystemConfiguration
  24. import Foundation
  25. public enum ReachabilityError: Error {
  26. case FailedToCreateWithAddress(sockaddr_in)
  27. case FailedToCreateWithHostname(String)
  28. case UnableToSetCallback
  29. case UnableToSetDispatchQueue
  30. }
  31. @available(*, unavailable, renamed: "Notification.Name.reachabilityChanged")
  32. public let ReachabilityChangedNotification = NSNotification.Name("ReachabilityChangedNotification")
  33. extension Notification.Name {
  34. public static let reachabilityChanged = Notification.Name("reachabilityChanged")
  35. }
  36. func callback(reachability: SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
  37. guard let info = info else { return }
  38. let reachability = Unmanaged<Reachability>.fromOpaque(info).takeUnretainedValue()
  39. reachability.reachabilityChanged()
  40. }
  41. public class Reachability {
  42. public typealias NetworkReachable = (Reachability) -> ()
  43. public typealias NetworkUnreachable = (Reachability) -> ()
  44. @available(*, unavailable, renamed: "Connection")
  45. public enum NetworkStatus: CustomStringConvertible {
  46. case notReachable, reachableViaWiFi, reachableViaWWAN
  47. public var description: String {
  48. switch self {
  49. case .reachableViaWWAN: return "Cellular"
  50. case .reachableViaWiFi: return "WiFi"
  51. case .notReachable: return "No Connection"
  52. }
  53. }
  54. }
  55. public enum Connection: CustomStringConvertible {
  56. case none, wifi, cellular
  57. public var description: String {
  58. switch self {
  59. case .cellular: return "Cellular"
  60. case .wifi: return "WiFi"
  61. case .none: return "No Connection"
  62. }
  63. }
  64. }
  65. public var whenReachable: NetworkReachable?
  66. public var whenUnreachable: NetworkUnreachable?
  67. @available(*, deprecated: 4.0, renamed: "allowsCellularConnection")
  68. public let reachableOnWWAN: Bool = true
  69. /// Set to `false` to force Reachability.connection to .none when on cellular connection (default value `true`)
  70. public var allowsCellularConnection: Bool
  71. // The notification center on which "reachability changed" events are being posted
  72. public var notificationCenter: NotificationCenter = NotificationCenter.default
  73. @available(*, deprecated: 4.0, renamed: "connection.description")
  74. public var currentReachabilityString: String {
  75. return "\(connection)"
  76. }
  77. @available(*, unavailable, renamed: "connection")
  78. public var currentReachabilityStatus: Connection {
  79. return connection
  80. }
  81. public var connection: Connection {
  82. guard isReachableFlagSet else { return .none }
  83. // If we're reachable, but not on an iOS device (i.e. simulator), we must be on WiFi
  84. guard isRunningOnDevice else { return .wifi }
  85. var connection = Connection.none
  86. if !isConnectionRequiredFlagSet {
  87. connection = .wifi
  88. }
  89. if isConnectionOnTrafficOrDemandFlagSet {
  90. if !isInterventionRequiredFlagSet {
  91. connection = .wifi
  92. }
  93. }
  94. if isOnWWANFlagSet {
  95. if !allowsCellularConnection {
  96. connection = .none
  97. } else {
  98. connection = .cellular
  99. }
  100. }
  101. return connection
  102. }
  103. fileprivate var previousFlags: SCNetworkReachabilityFlags?
  104. fileprivate var isRunningOnDevice: Bool = {
  105. #if targetEnvironment(simulator)
  106. return false
  107. #else
  108. return true
  109. #endif
  110. }()
  111. fileprivate var notifierRunning = false
  112. fileprivate let reachabilityRef: SCNetworkReachability
  113. fileprivate let reachabilitySerialQueue = DispatchQueue(label: "uk.co.ashleymills.reachability")
  114. fileprivate var usingHostname = false
  115. required public init(reachabilityRef: SCNetworkReachability, usingHostname: Bool = false) {
  116. allowsCellularConnection = true
  117. self.reachabilityRef = reachabilityRef
  118. self.usingHostname = usingHostname
  119. }
  120. public convenience init?(hostname: String) {
  121. guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else { return nil }
  122. self.init(reachabilityRef: ref, usingHostname: true)
  123. }
  124. public convenience init?() {
  125. var zeroAddress = sockaddr()
  126. zeroAddress.sa_len = UInt8(MemoryLayout<sockaddr>.size)
  127. zeroAddress.sa_family = sa_family_t(AF_INET)
  128. guard let ref = SCNetworkReachabilityCreateWithAddress(nil, &zeroAddress) else { return nil }
  129. self.init(reachabilityRef: ref)
  130. }
  131. deinit {
  132. stopNotifier()
  133. }
  134. }
  135. public extension Reachability {
  136. // MARK: - *** Notifier methods ***
  137. func startNotifier() throws {
  138. guard !notifierRunning else { return }
  139. var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
  140. context.info = UnsafeMutableRawPointer(Unmanaged<Reachability>.passUnretained(self).toOpaque())
  141. if !SCNetworkReachabilitySetCallback(reachabilityRef, callback, &context) {
  142. stopNotifier()
  143. throw ReachabilityError.UnableToSetCallback
  144. }
  145. if !SCNetworkReachabilitySetDispatchQueue(reachabilityRef, reachabilitySerialQueue) {
  146. stopNotifier()
  147. throw ReachabilityError.UnableToSetDispatchQueue
  148. }
  149. // Perform an initial check
  150. reachabilitySerialQueue.async {
  151. self.reachabilityChanged()
  152. }
  153. notifierRunning = true
  154. }
  155. func stopNotifier() {
  156. defer { notifierRunning = false }
  157. SCNetworkReachabilitySetCallback(reachabilityRef, nil, nil)
  158. SCNetworkReachabilitySetDispatchQueue(reachabilityRef, nil)
  159. }
  160. // MARK: - *** Connection test methods ***
  161. @available(*, deprecated: 4.0, message: "Please use `connection != .none`")
  162. var isReachable: Bool {
  163. guard isReachableFlagSet else { return false }
  164. if isConnectionRequiredAndTransientFlagSet {
  165. return false
  166. }
  167. if isRunningOnDevice {
  168. if isOnWWANFlagSet && !reachableOnWWAN {
  169. // We don't want to connect when on cellular connection
  170. return false
  171. }
  172. }
  173. return true
  174. }
  175. @available(*, deprecated: 4.0, message: "Please use `connection == .cellular`")
  176. var isReachableViaWWAN: Bool {
  177. // Check we're not on the simulator, we're REACHABLE and check we're on WWAN
  178. return isRunningOnDevice && isReachableFlagSet && isOnWWANFlagSet
  179. }
  180. @available(*, deprecated: 4.0, message: "Please use `connection == .wifi`")
  181. var isReachableViaWiFi: Bool {
  182. // Check we're reachable
  183. guard isReachableFlagSet else { return false }
  184. // If reachable we're reachable, but not on an iOS device (i.e. simulator), we must be on WiFi
  185. guard isRunningOnDevice else { return true }
  186. // Check we're NOT on WWAN
  187. return !isOnWWANFlagSet
  188. }
  189. var description: String {
  190. let W = isRunningOnDevice ? (isOnWWANFlagSet ? "W" : "-") : "X"
  191. let R = isReachableFlagSet ? "R" : "-"
  192. let c = isConnectionRequiredFlagSet ? "c" : "-"
  193. let t = isTransientConnectionFlagSet ? "t" : "-"
  194. let i = isInterventionRequiredFlagSet ? "i" : "-"
  195. let C = isConnectionOnTrafficFlagSet ? "C" : "-"
  196. let D = isConnectionOnDemandFlagSet ? "D" : "-"
  197. let l = isLocalAddressFlagSet ? "l" : "-"
  198. let d = isDirectFlagSet ? "d" : "-"
  199. return "\(W)\(R) \(c)\(t)\(i)\(C)\(D)\(l)\(d)"
  200. }
  201. }
  202. fileprivate extension Reachability {
  203. func reachabilityChanged() {
  204. guard previousFlags != flags else { return }
  205. let block = connection != .none ? whenReachable : whenUnreachable
  206. DispatchQueue.main.async {
  207. if self.usingHostname {
  208. print("USING HOSTNAME ABOUT TO CALL BLOCK")
  209. }
  210. block?(self)
  211. self.notificationCenter.post(name: .reachabilityChanged, object:self)
  212. }
  213. previousFlags = flags
  214. }
  215. var isOnWWANFlagSet: Bool {
  216. #if os(iOS)
  217. return flags.contains(.isWWAN)
  218. #else
  219. return false
  220. #endif
  221. }
  222. var isReachableFlagSet: Bool {
  223. return flags.contains(.reachable)
  224. }
  225. var isConnectionRequiredFlagSet: Bool {
  226. return flags.contains(.connectionRequired)
  227. }
  228. var isInterventionRequiredFlagSet: Bool {
  229. return flags.contains(.interventionRequired)
  230. }
  231. var isConnectionOnTrafficFlagSet: Bool {
  232. return flags.contains(.connectionOnTraffic)
  233. }
  234. var isConnectionOnDemandFlagSet: Bool {
  235. return flags.contains(.connectionOnDemand)
  236. }
  237. var isConnectionOnTrafficOrDemandFlagSet: Bool {
  238. return !flags.intersection([.connectionOnTraffic, .connectionOnDemand]).isEmpty
  239. }
  240. var isTransientConnectionFlagSet: Bool {
  241. return flags.contains(.transientConnection)
  242. }
  243. var isLocalAddressFlagSet: Bool {
  244. return flags.contains(.isLocalAddress)
  245. }
  246. var isDirectFlagSet: Bool {
  247. return flags.contains(.isDirect)
  248. }
  249. var isConnectionRequiredAndTransientFlagSet: Bool {
  250. return flags.intersection([.connectionRequired, .transientConnection]) == [.connectionRequired, .transientConnection]
  251. }
  252. var flags: SCNetworkReachabilityFlags {
  253. var flags = SCNetworkReachabilityFlags()
  254. if SCNetworkReachabilityGetFlags(reachabilityRef, &flags) {
  255. print("Returning flags \(flags)")
  256. return flags
  257. } else {
  258. return SCNetworkReachabilityFlags()
  259. }
  260. }
  261. }