-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkReachability
More file actions
35 lines (28 loc) · 983 Bytes
/
NetworkReachability
File metadata and controls
35 lines (28 loc) · 983 Bytes
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
import Foundation
import Network
class NetworkReachability {
static let shared = NetworkReachability()
let monitor = NWPathMonitor()
private var status: NWPath.Status = .requiresConnection
var isReachable: Bool { status == .satisfied }
var isReachableOnCellular: Bool = true
func startMonitoring() {
monitor.pathUpdateHandler = { [weak self] path in
self?.status = path.status
self?.isReachableOnCellular = path.isExpensive
if path.status == .satisfied {
Logger.log("We're connected!")
// post connected notification
} else {
Logger.log("No connection.")
// post disconnected notification
}
Logger.log(path.isExpensive)
}
let queue = DispatchQueue(label: "NetworkMonitor")
monitor.start(queue: queue)
}
func stopMonitoring() {
monitor.cancel()
}
}