61 lines
1.5 KiB
Swift
61 lines
1.5 KiB
Swift
//
|
|
// SPNetworkReachabilityManager.swift
|
|
// ShortPlay
|
|
//
|
|
// Created by Overseas on 2025/4/19.
|
|
//
|
|
|
|
import UIKit
|
|
import Network
|
|
import Combine
|
|
import Alamofire
|
|
|
|
class SPNetworkReachabilityManager {
|
|
// enum Status {
|
|
// case notReachable
|
|
// case reachableViaWiFi
|
|
// case reachableViaWWAN
|
|
// case ethernet
|
|
// }
|
|
|
|
static let manager: SPNetworkReachabilityManager = SPNetworkReachabilityManager()
|
|
|
|
private let reachabilityManager = NetworkReachabilityManager()
|
|
|
|
///是否有网
|
|
// @objc var isReachable: Bool {
|
|
// switch currentReachabilityStatus {
|
|
// case .notReachable:
|
|
// return false
|
|
// case .reachableViaWiFi, .reachableViaWWAN:
|
|
// return true
|
|
//
|
|
// default:
|
|
// return false
|
|
// }
|
|
// }
|
|
|
|
func startMonitoring() {
|
|
|
|
reachabilityManager?.startListening(onUpdatePerforming: { status in
|
|
switch status {
|
|
case .notReachable:
|
|
print("网络不可用")
|
|
case .unknown:
|
|
print("网络状态未知")
|
|
case .reachable(.cellular):
|
|
print("蜂窝网络连接")
|
|
case .reachable(.ethernetOrWiFi):
|
|
print("WiFi 或有线网络连接")
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extension SPNetworkReachabilityManager {
|
|
///网络发生变化
|
|
@objc static let reachabilityDidChangeNotification = NSNotification.Name(rawValue: "reachabilityDidChangeNotification")
|
|
}
|