|
| 1 | +import Foundation |
| 2 | +import Network |
| 3 | + |
| 4 | +/** |
| 5 | + * Network connection type. |
| 6 | + */ |
| 7 | +public enum ConnectionType: String, Sendable { |
| 8 | + case wifi = "WiFi" |
| 9 | + case cellular = "Cellular" |
| 10 | + case ethernet = "Ethernet" |
| 11 | + case loopback = "Loopback" |
| 12 | + case other = "Other" |
| 13 | + case unavailable = "Unavailable" |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Network connection status. |
| 18 | + */ |
| 19 | +public enum NetworkStatus: Sendable { |
| 20 | + case connected(ConnectionType) |
| 21 | + case disconnected |
| 22 | + case connecting |
| 23 | + case requiresConnection |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * A protocol for monitoring network connectivity. |
| 28 | + * |
| 29 | + * NetworkMonitor provides real-time information about the device's network |
| 30 | + * connection status, including connection type (WiFi, Cellular, etc.) and |
| 31 | + * availability. This allows your app to adapt its behavior based on network conditions. |
| 32 | + * |
| 33 | + * ## Usage |
| 34 | + * ```swift |
| 35 | + * let monitor = DefaultNetworkMonitor() |
| 36 | + * |
| 37 | + * // Check current status |
| 38 | + * let status = await monitor.currentStatus |
| 39 | + * if case .connected(let type) = status { |
| 40 | + * print("Connected via \(type)") |
| 41 | + * } |
| 42 | + * |
| 43 | + * // Observe status changes |
| 44 | + * for await status in monitor.statusUpdates { |
| 45 | + * switch status { |
| 46 | + * case .connected(let type): |
| 47 | + * print("Connected via \(type)") |
| 48 | + * case .disconnected: |
| 49 | + * print("No connection") |
| 50 | + * default: |
| 51 | + * break |
| 52 | + * } |
| 53 | + * } |
| 54 | + * ``` |
| 55 | + */ |
| 56 | +public protocol NetworkMonitor: Sendable { |
| 57 | + /// The current network status |
| 58 | + var currentStatus: NetworkStatus { get async } |
| 59 | + |
| 60 | + /// An async sequence of network status updates |
| 61 | + var statusUpdates: AsyncStream<NetworkStatus> { get } |
| 62 | + |
| 63 | + /// Whether the device is currently connected to the internet |
| 64 | + var isConnected: Bool { get async } |
| 65 | + |
| 66 | + /// The current connection type, if connected |
| 67 | + var connectionType: ConnectionType? { get async } |
| 68 | + |
| 69 | + /// Starts monitoring network connectivity |
| 70 | + func startMonitoring() |
| 71 | + |
| 72 | + /// Stops monitoring network connectivity |
| 73 | + func stopMonitoring() |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Default implementation of NetworkMonitor using Network Framework. |
| 78 | + * |
| 79 | + * This monitor uses NWPathMonitor to track network connectivity and provides |
| 80 | + * real-time updates about connection status and type. |
| 81 | + */ |
| 82 | +public class DefaultNetworkMonitor: NetworkMonitor, @unchecked Sendable { |
| 83 | + private let monitor: NWPathMonitor |
| 84 | + private let queue: DispatchQueue |
| 85 | + private var statusContinuation: AsyncStream<NetworkStatus>.Continuation? |
| 86 | + private var currentStatusValue: NetworkStatus = .disconnected |
| 87 | + |
| 88 | + public init(requiredInterfaceType: NWInterface.InterfaceType? = nil) { |
| 89 | + if let interfaceType = requiredInterfaceType { |
| 90 | + self.monitor = NWPathMonitor(requiredInterfaceType: interfaceType) |
| 91 | + } else { |
| 92 | + self.monitor = NWPathMonitor() |
| 93 | + } |
| 94 | + self.queue = DispatchQueue(label: "com.networkkit.monitor") |
| 95 | + } |
| 96 | + |
| 97 | + public var currentStatus: NetworkStatus { |
| 98 | + get async { |
| 99 | + return await withCheckedContinuation { continuation in |
| 100 | + queue.async { |
| 101 | + continuation.resume(returning: self.currentStatusValue) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public var statusUpdates: AsyncStream<NetworkStatus> { |
| 108 | + AsyncStream { continuation in |
| 109 | + statusContinuation = continuation |
| 110 | + |
| 111 | + monitor.pathUpdateHandler = { [weak self] path in |
| 112 | + guard let self = self else { return } |
| 113 | + |
| 114 | + let status = self.status(from: path) |
| 115 | + self.currentStatusValue = status |
| 116 | + |
| 117 | + self.queue.async { |
| 118 | + continuation.yield(status) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + continuation.onTermination = { [weak self] _ in |
| 123 | + self?.stopMonitoring() |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public var isConnected: Bool { |
| 129 | + get async { |
| 130 | + let status = await currentStatus |
| 131 | + if case .connected = status { |
| 132 | + return true |
| 133 | + } |
| 134 | + return false |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public var connectionType: ConnectionType? { |
| 139 | + get async { |
| 140 | + let status = await currentStatus |
| 141 | + if case .connected(let type) = status { |
| 142 | + return type |
| 143 | + } |
| 144 | + return nil |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + public func startMonitoring() { |
| 149 | + monitor.start(queue: queue) |
| 150 | + } |
| 151 | + |
| 152 | + public func stopMonitoring() { |
| 153 | + monitor.cancel() |
| 154 | + statusContinuation?.finish() |
| 155 | + statusContinuation = nil |
| 156 | + } |
| 157 | + |
| 158 | + private func status(from path: NWPath) -> NetworkStatus { |
| 159 | + guard path.status == .satisfied else { |
| 160 | + if path.status == .requiresConnection { |
| 161 | + return .requiresConnection |
| 162 | + } |
| 163 | + return .disconnected |
| 164 | + } |
| 165 | + |
| 166 | + // Determine connection type |
| 167 | + let connectionType: ConnectionType |
| 168 | + |
| 169 | + if path.usesInterfaceType(.wifi) { |
| 170 | + connectionType = .wifi |
| 171 | + } else if path.usesInterfaceType(.cellular) { |
| 172 | + connectionType = .cellular |
| 173 | + } else if path.usesInterfaceType(.wiredEthernet) { |
| 174 | + connectionType = .ethernet |
| 175 | + } else if path.usesInterfaceType(.loopback) { |
| 176 | + connectionType = .loopback |
| 177 | + } else { |
| 178 | + connectionType = .other |
| 179 | + } |
| 180 | + |
| 181 | + return .connected(connectionType) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * A network monitor that only checks WiFi connectivity. |
| 187 | + */ |
| 188 | +public final class WiFiNetworkMonitor: DefaultNetworkMonitor, @unchecked Sendable { |
| 189 | + public init() { |
| 190 | + super.init(requiredInterfaceType: .wifi) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +/** |
| 195 | + * A network monitor that only checks cellular connectivity. |
| 196 | + */ |
| 197 | +public final class CellularNetworkMonitor: DefaultNetworkMonitor, @unchecked Sendable { |
| 198 | + public init() { |
| 199 | + super.init(requiredInterfaceType: .cellular) |
| 200 | + } |
| 201 | +} |
0 commit comments