TSNetworkTool.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // TSNetworkTool.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2025/1/2.
  6. //
  7. import Alamofire
  8. import Network
  9. class TSNetworkTool {
  10. static let shared = TSNetworkTool()
  11. func monitorNetworkPermission(escapable result: @escaping (Bool) -> Void) {
  12. let monitor = NWPathMonitor()
  13. let queue = DispatchQueue.global(qos: .background)
  14. monitor.start(queue: queue)
  15. monitor.pathUpdateHandler = { path in
  16. DispatchQueue.main.async {
  17. if path.status == .satisfied {
  18. debugPrint("网络可用,用户同意了权限")
  19. result(true)
  20. } else {
  21. debugPrint("网络不可用,可能用户拒绝了权限")
  22. result(false)
  23. }
  24. }
  25. }
  26. }
  27. func startListenNetStatus(handler: @escaping (NetworkReachabilityManager.NetworkReachabilityStatus, NetworkReachabilityManager?) -> Void) {
  28. // 创建 NetworkReachabilityManager 实例
  29. let networkManager = NetworkReachabilityManager.default
  30. // 开始监听网络状态
  31. networkManager?.startListening { status in
  32. handler(status, networkManager)
  33. }
  34. }
  35. }