网络状态监听优化
This commit is contained in:
parent
a399554e58
commit
73512c7ef1
@ -11,38 +11,14 @@ import Combine
|
||||
import Alamofire
|
||||
|
||||
class SPNetworkReachabilityManager {
|
||||
// enum Status {
|
||||
// case notReachable
|
||||
// case reachableViaWiFi
|
||||
// case reachableViaWWAN
|
||||
// case ethernet
|
||||
// }
|
||||
|
||||
static let manager: SPNetworkReachabilityManager = SPNetworkReachabilityManager()
|
||||
|
||||
///是否有网
|
||||
var isReachable: Bool?
|
||||
|
||||
/*
|
||||
private let reachabilityManager = NetworkReachabilityManager()
|
||||
|
||||
func startMonitoring() {
|
||||
|
||||
reachabilityManager?.startListening(onUpdatePerforming: { status in
|
||||
switch status {
|
||||
case .notReachable:
|
||||
print("网络不可用")
|
||||
case .unknown:
|
||||
print("网络状态未知")
|
||||
case .reachable(.cellular):
|
||||
print("蜂窝网络连接")
|
||||
case .reachable(.ethernetOrWiFi):
|
||||
print("WiFi 或有线网络连接")
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
*/
|
||||
private(set) var connectionType: NWInterface.InterfaceType?
|
||||
private(set) var status: NWPath.Status?
|
||||
|
||||
private let monitor = NWPathMonitor()
|
||||
private let queue = DispatchQueue(label: "NetworkMonitorQueue")
|
||||
@ -51,9 +27,24 @@ class SPNetworkReachabilityManager {
|
||||
|
||||
monitor.pathUpdateHandler = { [weak self] path in
|
||||
guard let self = self else { return }
|
||||
if path.status == .satisfied {
|
||||
self.status = path.status
|
||||
|
||||
if path.usesInterfaceType(.wifi) {
|
||||
self.connectionType = .wifi
|
||||
vpLog(message: "+++++++++++++++网络变化==wifi")
|
||||
} else if path.usesInterfaceType(.cellular) {
|
||||
self.connectionType = .cellular
|
||||
vpLog(message: "+++++++++++++++网络变化==cellular")
|
||||
} else if path.usesInterfaceType(.wiredEthernet) {
|
||||
self.connectionType = .wiredEthernet
|
||||
vpLog(message: "+++++++++++++++网络变化==wiredEthernet")
|
||||
} else {
|
||||
self.connectionType = nil
|
||||
vpLog(message: "+++++++++++++++网络变化==???")
|
||||
}
|
||||
|
||||
if path.status == .satisfied, self.connectionType != nil {
|
||||
if self.isReachable == false {
|
||||
print("++++++有网")
|
||||
self.isReachable = true
|
||||
DispatchQueue.main.async {
|
||||
NotificationCenter.default.post(name: SPNetworkReachabilityManager.reachabilityDidChangeNotification, object: nil)
|
||||
@ -64,7 +55,6 @@ class SPNetworkReachabilityManager {
|
||||
|
||||
} else {
|
||||
if self.isReachable == true {
|
||||
print("++++++无网")
|
||||
self.isReachable = false
|
||||
DispatchQueue.main.async {
|
||||
NotificationCenter.default.post(name: SPNetworkReachabilityManager.reachabilityDidChangeNotification, object: nil)
|
||||
@ -73,14 +63,6 @@ class SPNetworkReachabilityManager {
|
||||
self.isReachable = false
|
||||
}
|
||||
}
|
||||
|
||||
// if path.usesInterfaceType(.wifi) {
|
||||
// print("++++++Using Wi-Fi")
|
||||
// }
|
||||
//
|
||||
// if path.usesInterfaceType(.cellular) {
|
||||
// print("++++++Using Cellular")
|
||||
// }
|
||||
}
|
||||
|
||||
monitor.start(queue: queue)
|
||||
|
Loading…
x
Reference in New Issue
Block a user