73 lines
1.7 KiB
Swift
73 lines
1.7 KiB
Swift
//
|
|
// NetworkObserver.swift
|
|
// Thimra
|
|
//
|
|
// Created by Overseas on 2025/4/21.
|
|
//
|
|
|
|
import UIKit
|
|
import Network
|
|
import Reachability
|
|
|
|
|
|
class NetworkObserver {
|
|
static let share = NetworkObserver()
|
|
|
|
|
|
private let monitor = NWPathMonitor()
|
|
private let queue = DispatchQueue(label: "NetworkMonitorQueue")
|
|
|
|
func startMonitoring() {
|
|
monitor.pathUpdateHandler = { path in
|
|
if path.status == .satisfied {
|
|
print("++++++有网")
|
|
} else {
|
|
print("++++++无网")
|
|
}
|
|
|
|
// if path.usesInterfaceType(.wifi) {
|
|
// print("++++++Using Wi-Fi")
|
|
// }
|
|
//
|
|
// if path.usesInterfaceType(.cellular) {
|
|
// print("++++++Using Cellular")
|
|
// }
|
|
}
|
|
|
|
monitor.start(queue: queue)
|
|
}
|
|
|
|
func stopMonitoring() {
|
|
monitor.cancel()
|
|
}
|
|
|
|
/*
|
|
private let reachability = try! Reachability()
|
|
|
|
func startMonitoring() {
|
|
reachability.whenReachable = { reachability in
|
|
if reachability.connection == .wifi {
|
|
print("++++++Network reachable via Wi-Fi")
|
|
} else if reachability.connection == .cellular {
|
|
print("++++++Network reachable via Cellular")
|
|
}
|
|
}
|
|
|
|
reachability.whenUnreachable = { _ in
|
|
print("++++++Network not reachable")
|
|
}
|
|
|
|
do {
|
|
try reachability.startNotifier()
|
|
} catch {
|
|
print("++++++Unable to start notifier")
|
|
}
|
|
}
|
|
|
|
func stopMonitoring() {
|
|
reachability.stopNotifier()
|
|
}
|
|
*/
|
|
|
|
}
|