Update for swift 3.
- Convert code to swift 3 - Update alamofire to 4.0.1 for swift 3.
This commit is contained in:
171
Pods/Alamofire/Source/NetworkReachabilityManager.swift
generated
171
Pods/Alamofire/Source/NetworkReachabilityManager.swift
generated
@ -27,15 +27,13 @@
|
||||
import Foundation
|
||||
import SystemConfiguration
|
||||
|
||||
/**
|
||||
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 {
|
||||
/// 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.
|
||||
open class NetworkReachabilityManager {
|
||||
/**
|
||||
Defines the various states of network reachability.
|
||||
|
||||
@ -44,49 +42,54 @@ public class NetworkReachabilityManager {
|
||||
- ReachableOnWWAN: The network is reachable over the WWAN connection.
|
||||
- ReachableOnWiFi: The network is reachable over the WiFi connection.
|
||||
*/
|
||||
|
||||
|
||||
/// Defines the various states of network reachability.
|
||||
///
|
||||
/// - unknown: It is unknown whether the network is reachable.
|
||||
/// - notReachable: The network is not reachable.
|
||||
/// - reachable: The network is reachable.
|
||||
public enum NetworkReachabilityStatus {
|
||||
case Unknown
|
||||
case NotReachable
|
||||
case Reachable(ConnectionType)
|
||||
case unknown
|
||||
case notReachable
|
||||
case reachable(ConnectionType)
|
||||
}
|
||||
|
||||
/**
|
||||
Defines the various connection types detected by reachability flags.
|
||||
|
||||
- EthernetOrWiFi: The connection type is either over Ethernet or WiFi.
|
||||
- WWAN: The connection type is a WWAN connection.
|
||||
*/
|
||||
/// Defines the various connection types detected by reachability flags.
|
||||
///
|
||||
/// - ethernetOrWiFi: The connection type is either over Ethernet or WiFi.
|
||||
/// - wwan: The connection type is a WWAN connection.
|
||||
public enum ConnectionType {
|
||||
case EthernetOrWiFi
|
||||
case WWAN
|
||||
case ethernetOrWiFi
|
||||
case wwan
|
||||
}
|
||||
|
||||
/// A closure executed when the network reachability status changes. The closure takes a single argument: the
|
||||
/// A closure executed when the network reachability status changes. The closure takes a single argument: the
|
||||
/// network reachability status.
|
||||
public typealias Listener = NetworkReachabilityStatus -> Void
|
||||
public typealias Listener = (NetworkReachabilityStatus) -> Void
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
/// Whether the network is currently reachable.
|
||||
public var isReachable: Bool { return isReachableOnWWAN || isReachableOnEthernetOrWiFi }
|
||||
open var isReachable: Bool { return isReachableOnWWAN || isReachableOnEthernetOrWiFi }
|
||||
|
||||
/// Whether the network is currently reachable over the WWAN interface.
|
||||
public var isReachableOnWWAN: Bool { return networkReachabilityStatus == .Reachable(.WWAN) }
|
||||
open var isReachableOnWWAN: Bool { return networkReachabilityStatus == .reachable(.wwan) }
|
||||
|
||||
/// Whether the network is currently reachable over Ethernet or WiFi interface.
|
||||
public var isReachableOnEthernetOrWiFi: Bool { return networkReachabilityStatus == .Reachable(.EthernetOrWiFi) }
|
||||
open var isReachableOnEthernetOrWiFi: Bool { return networkReachabilityStatus == .reachable(.ethernetOrWiFi) }
|
||||
|
||||
/// The current network reachability status.
|
||||
public var networkReachabilityStatus: NetworkReachabilityStatus {
|
||||
guard let flags = self.flags else { return .Unknown }
|
||||
open 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()
|
||||
open var listenerQueue: DispatchQueue = DispatchQueue.main
|
||||
|
||||
/// A closure executed when the network reachability status changes.
|
||||
public var listener: Listener?
|
||||
open var listener: Listener?
|
||||
|
||||
private var flags: SCNetworkReachabilityFlags? {
|
||||
var flags = SCNetworkReachabilityFlags()
|
||||
@ -103,45 +106,34 @@ 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.
|
||||
*/
|
||||
/// 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 IPv4 or IPv6 address.
|
||||
|
||||
- returns: The new `NetworkReachabilityManager` instance.
|
||||
*/
|
||||
/// Creates a `NetworkReachabilityManager` instance that monitors the address 0.0.0.0.
|
||||
///
|
||||
/// Reachability treats the 0.0.0.0 address as a special token that causes it to monitor the general routing
|
||||
/// status of the device, both IPv4 and IPv6.
|
||||
///
|
||||
/// - returns: The new `NetworkReachabilityManager` instance.
|
||||
public convenience init?() {
|
||||
if #available(iOS 9.0, OSX 10.10, *) {
|
||||
var address = sockaddr_in6()
|
||||
address.sin6_len = UInt8(sizeofValue(address))
|
||||
address.sin6_family = sa_family_t(AF_INET6)
|
||||
var address = sockaddr_in()
|
||||
address.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
|
||||
address.sin_family = sa_family_t(AF_INET)
|
||||
|
||||
guard let reachability = withUnsafePointer(&address, {
|
||||
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
|
||||
}) else { return nil }
|
||||
guard let reachability = withUnsafePointer(to: &address, { pointer in
|
||||
return pointer.withMemoryRebound(to: sockaddr.self, capacity: MemoryLayout<sockaddr>.size) {
|
||||
return SCNetworkReachabilityCreateWithAddress(nil, $0)
|
||||
}
|
||||
}) else { return nil }
|
||||
|
||||
self.init(reachability: reachability)
|
||||
} else {
|
||||
var address = sockaddr_in()
|
||||
address.sin_len = UInt8(sizeofValue(address))
|
||||
address.sin_family = sa_family_t(AF_INET)
|
||||
|
||||
guard let reachability = withUnsafePointer(&address, {
|
||||
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
|
||||
}) else { return nil }
|
||||
|
||||
self.init(reachability: reachability)
|
||||
}
|
||||
self.init(reachability: reachability)
|
||||
}
|
||||
|
||||
private init(reachability: SCNetworkReachability) {
|
||||
@ -155,19 +147,18 @@ 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 {
|
||||
/// Starts listening for changes in network reachability status.
|
||||
///
|
||||
/// - returns: `true` if listening was started successfully, `false` otherwise.
|
||||
@discardableResult
|
||||
open func startListening() -> Bool {
|
||||
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
|
||||
context.info = UnsafeMutablePointer(Unmanaged.passUnretained(self).toOpaque())
|
||||
context.info = Unmanaged.passUnretained(self).toOpaque()
|
||||
|
||||
let callbackEnabled = SCNetworkReachabilitySetCallback(
|
||||
reachability,
|
||||
{ (_, flags, info) in
|
||||
let reachability = Unmanaged<NetworkReachabilityManager>.fromOpaque(COpaquePointer(info)).takeUnretainedValue()
|
||||
let reachability = Unmanaged<NetworkReachabilityManager>.fromOpaque(info!).takeUnretainedValue()
|
||||
reachability.notifyListener(flags)
|
||||
},
|
||||
&context
|
||||
@ -175,7 +166,7 @@ public class NetworkReachabilityManager {
|
||||
|
||||
let queueEnabled = SCNetworkReachabilitySetDispatchQueue(reachability, listenerQueue)
|
||||
|
||||
dispatch_async(listenerQueue) {
|
||||
listenerQueue.async {
|
||||
self.previousFlags = SCNetworkReachabilityFlags()
|
||||
self.notifyListener(self.flags ?? SCNetworkReachabilityFlags())
|
||||
}
|
||||
@ -183,17 +174,15 @@ public class NetworkReachabilityManager {
|
||||
return callbackEnabled && queueEnabled
|
||||
}
|
||||
|
||||
/**
|
||||
Stops listening for changes in network reachability status.
|
||||
*/
|
||||
public func stopListening() {
|
||||
/// Stops listening for changes in network reachability status.
|
||||
open func stopListening() {
|
||||
SCNetworkReachabilitySetCallback(reachability, nil, nil)
|
||||
SCNetworkReachabilitySetDispatchQueue(reachability, nil)
|
||||
}
|
||||
|
||||
// MARK: - Internal - Listener Notification
|
||||
|
||||
func notifyListener(flags: SCNetworkReachabilityFlags) {
|
||||
func notifyListener(_ flags: SCNetworkReachabilityFlags) {
|
||||
guard previousFlags != flags else { return }
|
||||
previousFlags = flags
|
||||
|
||||
@ -202,19 +191,19 @@ public class NetworkReachabilityManager {
|
||||
|
||||
// MARK: - Internal - Network Reachability Status
|
||||
|
||||
func networkReachabilityStatusForFlags(flags: SCNetworkReachabilityFlags) -> NetworkReachabilityStatus {
|
||||
guard flags.contains(.Reachable) else { return .NotReachable }
|
||||
func networkReachabilityStatusForFlags(_ flags: SCNetworkReachabilityFlags) -> NetworkReachabilityStatus {
|
||||
guard flags.contains(.reachable) else { return .notReachable }
|
||||
|
||||
var networkStatus: NetworkReachabilityStatus = .NotReachable
|
||||
var networkStatus: NetworkReachabilityStatus = .notReachable
|
||||
|
||||
if !flags.contains(.ConnectionRequired) { networkStatus = .Reachable(.EthernetOrWiFi) }
|
||||
if !flags.contains(.connectionRequired) { networkStatus = .reachable(.ethernetOrWiFi) }
|
||||
|
||||
if flags.contains(.ConnectionOnDemand) || flags.contains(.ConnectionOnTraffic) {
|
||||
if !flags.contains(.InterventionRequired) { networkStatus = .Reachable(.EthernetOrWiFi) }
|
||||
if flags.contains(.connectionOnDemand) || flags.contains(.connectionOnTraffic) {
|
||||
if !flags.contains(.interventionRequired) { networkStatus = .reachable(.ethernetOrWiFi) }
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
if flags.contains(.IsWWAN) { networkStatus = .Reachable(.WWAN) }
|
||||
if flags.contains(.isWWAN) { networkStatus = .reachable(.wwan) }
|
||||
#endif
|
||||
|
||||
return networkStatus
|
||||
@ -225,25 +214,23 @@ public class NetworkReachabilityManager {
|
||||
|
||||
extension NetworkReachabilityManager.NetworkReachabilityStatus: Equatable {}
|
||||
|
||||
/**
|
||||
Returns whether the two network reachability status values are equal.
|
||||
|
||||
- parameter lhs: The left-hand side value to compare.
|
||||
- parameter rhs: The right-hand side value to compare.
|
||||
|
||||
- returns: `true` if the two values are equal, `false` otherwise.
|
||||
*/
|
||||
/// Returns whether the two network reachability status values are equal.
|
||||
///
|
||||
/// - parameter lhs: The left-hand side value to compare.
|
||||
/// - parameter rhs: The right-hand side value to compare.
|
||||
///
|
||||
/// - returns: `true` if the two values are equal, `false` otherwise.
|
||||
public func ==(
|
||||
lhs: NetworkReachabilityManager.NetworkReachabilityStatus,
|
||||
rhs: NetworkReachabilityManager.NetworkReachabilityStatus)
|
||||
-> Bool
|
||||
{
|
||||
switch (lhs, rhs) {
|
||||
case (.Unknown, .Unknown):
|
||||
case (.unknown, .unknown):
|
||||
return true
|
||||
case (.NotReachable, .NotReachable):
|
||||
case (.notReachable, .notReachable):
|
||||
return true
|
||||
case let (.Reachable(lhsConnectionType), .Reachable(rhsConnectionType)):
|
||||
case let (.reachable(lhsConnectionType), .reachable(rhsConnectionType)):
|
||||
return lhsConnectionType == rhsConnectionType
|
||||
default:
|
||||
return false
|
||||
|
Reference in New Issue
Block a user