-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTNetworkMonitoring.swift
More file actions
40 lines (34 loc) · 1.09 KB
/
STNetworkMonitoring.swift
File metadata and controls
40 lines (34 loc) · 1.09 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
//
// STNetworkMonitoring.swift
// STBaseProject
//
// Created by 寒江孤影 on 2018/12/10.
//
import UIKit
import Network
public enum STNetworkStatus: Int, @unchecked Sendable {
case WiFi = 0
case Cellular = 1
case NoNetwork = 2
}
public class STNetworkMonitoring: NSObject {
private let monitor = NWPathMonitor()
private let queue = DispatchQueue(label: "STNetworkMonitoring")
public func st_startMonitoring(networkStatusChanged: @escaping ((STNetworkStatus, String) -> Void)) {
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
if path.usesInterfaceType(.wifi) {
networkStatusChanged(.WiFi, "Connected to Wi-Fi")
} else if path.usesInterfaceType(.cellular) {
networkStatusChanged(.Cellular, "Connected to Cellular")
}
} else {
networkStatusChanged(.NoNetwork, "No network connection")
}
}
monitor.start(queue: queue)
}
public func st_stopMonitoring() {
monitor.cancel()
}
}