|
|
@@ -23,8 +23,18 @@
|
|
|
import Foundation
|
|
|
import SystemConfiguration
|
|
|
|
|
|
+/// Notification posted when network reachability status changes. The notification `object` contains the update
|
|
|
+/// network reachability status as an `NSNumber` which will need to be converted.
|
|
|
public let NetworkReachabilityStatusDidChangeNotification = "com.alamofire.network.reachability.status.did.change"
|
|
|
|
|
|
+/**
|
|
|
+ Defines the various states of network reachability.
|
|
|
+
|
|
|
+ - Unknown: It is unknown whether the network is reachable.
|
|
|
+ - NotReachable: The network is not reachable.
|
|
|
+ - ReachableOnWWAN: The network is reachable over the WWAN connection.
|
|
|
+ - ReachableOnWiFi: The network is reachable over the WiFi connection.
|
|
|
+*/
|
|
|
public enum NetworkReachabilityStatus: Int {
|
|
|
case Unknown = -1
|
|
|
case NotReachable = 0
|
|
|
@@ -32,21 +42,40 @@ public enum NetworkReachabilityStatus: Int {
|
|
|
case ReachableOnWiFi = 2
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ The `NetworkReachabilityManager` class listens for reachability changes of hosts and addresses for both WWAN and
|
|
|
+ WiFi network interfaces.
|
|
|
+
|
|
|
+ Reachability can be used to determine background information about why a network operation failed, or to retry
|
|
|
+ network requests when a connection is established. It should not be used to prevent a user from initiating a network
|
|
|
+ request, as it's possible that an initial request may be required to establish reachability.
|
|
|
+*/
|
|
|
public class NetworkReachabilityManager {
|
|
|
+ /// A closure executed when the network reachability status changes. The closure takes a single argument: the
|
|
|
+ /// network reachability status.
|
|
|
public typealias Listener = NetworkReachabilityStatus -> Void
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
+ /// Whether the network is currently reachable.
|
|
|
public var isReachable: Bool { return isReachableOnWWAN || isReachableOnWiFi }
|
|
|
+
|
|
|
+ /// Whether the network is currently reachable over the WWAN interface.
|
|
|
public var isReachableOnWWAN: Bool { return networkReachabilityStatus == .ReachableOnWWAN }
|
|
|
+
|
|
|
+ /// Whether the network is currently reachable over the WiFi interface.
|
|
|
public var isReachableOnWiFi: Bool { return networkReachabilityStatus == .ReachableOnWiFi }
|
|
|
|
|
|
+ /// The current network reachability status.
|
|
|
public var networkReachabilityStatus: NetworkReachabilityStatus {
|
|
|
guard let flags = self.flags else { return .Unknown }
|
|
|
return networkReachabilityStatusForFlags(flags)
|
|
|
}
|
|
|
|
|
|
+ /// The dispatch queue to execute the `listener` closure on.
|
|
|
public var listenerQueue: dispatch_queue_t = dispatch_get_main_queue()
|
|
|
+
|
|
|
+ /// A closure executed when the network reachability status changes.
|
|
|
public var listener: Listener?
|
|
|
|
|
|
private var flags: SCNetworkReachabilityFlags? {
|
|
|
@@ -64,11 +93,23 @@ public class NetworkReachabilityManager {
|
|
|
|
|
|
// MARK: - Initialization
|
|
|
|
|
|
+ /**
|
|
|
+ Creates a `NetworkReachabilityManager` instance with the specified host.
|
|
|
+
|
|
|
+ - parameter host: The host used to evaluate network reachability.
|
|
|
+
|
|
|
+ - returns: The new `NetworkReachabilityManager` instance.
|
|
|
+ */
|
|
|
public convenience init?(host: String) {
|
|
|
guard let reachability = SCNetworkReachabilityCreateWithName(nil, host) else { return nil }
|
|
|
self.init(reachability: reachability)
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Creates a `NetworkReachabilityManager` instance with the default socket address (`sockaddr_in6`).
|
|
|
+
|
|
|
+ - returns: The new `NetworkReachabilityManager` instance.
|
|
|
+ */
|
|
|
public convenience init?() {
|
|
|
var address = sockaddr_in6()
|
|
|
address.sin6_len = UInt8(sizeofValue(address))
|
|
|
@@ -92,6 +133,11 @@ public class NetworkReachabilityManager {
|
|
|
|
|
|
// MARK: - Listening
|
|
|
|
|
|
+ /**
|
|
|
+ Starts listening for changes in network reachability status.
|
|
|
+
|
|
|
+ - returns: `true` if listening was started successfully, `false` otherwise.
|
|
|
+ */
|
|
|
public func startListening() -> Bool {
|
|
|
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
|
|
|
context.info = UnsafeMutablePointer(Unmanaged.passUnretained(self).toOpaque())
|
|
|
@@ -114,6 +160,9 @@ public class NetworkReachabilityManager {
|
|
|
return callbackEnabled && queueEnabled
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Stops listening for changes in network reachability status.
|
|
|
+ */
|
|
|
public func stopListening() {
|
|
|
SCNetworkReachabilitySetCallback(reachability, nil, nil)
|
|
|
SCNetworkReachabilitySetDispatchQueue(reachability, nil)
|