Repository navigation
Expand file tree
/
Copy pathNetworkMonitor.swift
More file actions
112 lines (95 loc) · 3.28 KB
/
Copy pathNetworkMonitor.swift
File metadata and controls
112 lines (95 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// NetworkMonitor.swift
//
// Created by Ben Gohlke on August 27, 2025
//
// Copyright ©2025 Adapty. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import Network
enum NetworkQuality: Int, Comparable, CustomStringConvertible {
case excellent = 0
case good
case poor
case unavailable
var description: String {
switch self {
case .excellent: "Excellent"
case .good: "Good"
case .poor: "Poor"
case .unavailable: "Unavailable"
}
}
static func < (lhs: NetworkQuality, rhs: NetworkQuality) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
@Observable
class NetworkMonitor {
private(set) var quality: NetworkQuality = .unavailable
private let pathMonitor = NWPathMonitor()
private let monitorQueue = DispatchQueue(label: "NetworkMonitor", qos: .background)
init() {
startMonitoring()
}
deinit {
stopMonitoring()
}
private func startMonitoring() {
pathMonitor.pathUpdateHandler = { [weak self] path in
Task { @MainActor in
let quality = self?.assessNetworkQuality(path: path) ?? .unavailable
self?.quality = quality
}
}
pathMonitor.start(queue: monitorQueue)
}
private nonisolated func stopMonitoring() {
pathMonitor.cancel()
}
private nonisolated func assessNetworkQuality(path: NWPath) -> NetworkQuality {
guard path.status == .satisfied else {
return .unavailable
}
// WiFi and wired connections are generally excellent
if path.usesInterfaceType(.wifi) || path.usesInterfaceType(.wiredEthernet) {
return .excellent
}
// Cellular connections need more assessment
if path.usesInterfaceType(.cellular) {
return assessCellularQuality(path: path)
}
// Other connection types (like hotspot) default to good
return .good
}
private nonisolated func assessCellularQuality(path: NWPath) -> NetworkQuality {
// Check for expensive connection (often indicates limited data)
if path.isExpensive {
return .poor
}
// Check for constrained connection (iOS may limit usage)
if path.isConstrained {
return .poor
}
// If we have cellular without constraints, assume good quality
return .good
}
}