ReachabilitySwiftManager.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // ReachabilitySwiftManager.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/5/28.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import Reachability
  10. class ReachabilitySwiftManager: NSObject {
  11. static let shared : ReachabilitySwiftManager = ReachabilitySwiftManager()
  12. public lazy var reachability: Reachability = {
  13. let reachability = Reachability()!
  14. return reachability
  15. }()
  16. /// 初始化网络监听
  17. public func initStartReachability() {
  18. whenReachable()
  19. whenUnreachable()
  20. startNotifier()
  21. }
  22. /// 初始化网络监听需要他停止监听
  23. public func initStartStopReachability() {
  24. whenReachableStopNotifier()
  25. whenUnreachableStopNotifier()
  26. startNotifier()
  27. }
  28. /// 监听到连接
  29. private func whenReachable() {
  30. reachability.whenReachable = { reachability in
  31. if reachability.connection == .wifi {
  32. NXLLog("Reachable via WiFi")
  33. } else if reachability.connection == .cellular {
  34. NXLLog("Reachable via Cellular")
  35. }
  36. }
  37. }
  38. /// 监听到没有连接
  39. private func whenUnreachable() {
  40. reachability.whenUnreachable = { [weak self] _ in
  41. self?.alert()
  42. NXLLog("Not reachable")
  43. }
  44. }
  45. /// 监听到连接需要停止监听
  46. private func whenReachableStopNotifier() {
  47. reachability.whenReachable = { [weak self] reachability in
  48. if reachability.connection == .wifi {
  49. NXLLog("Reachable via WiFi")
  50. } else if reachability.connection == .cellular {
  51. NXLLog("Reachable via Cellular")
  52. }
  53. self?.stopNotifier()
  54. }
  55. }
  56. /// 监听到没有连接需要停止监听
  57. private func whenUnreachableStopNotifier() {
  58. reachability.whenUnreachable = { _ in
  59. AlertSheetView.alert(title: "网络连接失败", detailTitle: "检测到网络权限可能未开启,您可以在\"设置\"中检查蜂窝移动网络", cancelTitle: "取消", sureTitle: "设置", cancelBlock: {[weak self] (popupView, index, str) in
  60. self?.stopNotifier()
  61. }, confirmBlock: { (popupView, index, str) in
  62. DispatchQueue.main.async(execute: { [weak self] () -> Void in
  63. let url = URL(string: UIApplication.openSettingsURLString)
  64. if let url = url, UIApplication.shared.canOpenURL(url) { if #available(iOS 10.0, *) {
  65. UIApplication.shared.open(url, options: [:],completionHandler: {
  66. [weak self] (success) in
  67. self?.stopNotifier()
  68. })
  69. }else {
  70. UIApplication.shared.openURL(url)
  71. self?.stopNotifier()
  72. }
  73. }
  74. })
  75. })
  76. NXLLog("Not reachable")
  77. }
  78. }
  79. /// 开始监听
  80. private func startNotifier() {
  81. do {
  82. try reachability.startNotifier()
  83. } catch {
  84. startNotifier()
  85. NXLLog("Unable to start notifier")
  86. }
  87. }
  88. /// 停止监听
  89. private func stopNotifier() {
  90. reachability.stopNotifier()
  91. }
  92. /// 弹框跳转设置页面
  93. public func alert(){
  94. AlertSheetView.alert(title: "网络连接失败", detailTitle: "检测到网络权限可能未开启\n您可以在\"设置>无线数据>WLAN或WLAN与蜂窝移动网\"开启一下吧", cancelTitle: "取消", sureTitle: "设置", cancelBlock:nil, confirmBlock: { (popupView, index, str) in
  95. DispatchQueue.main.async(execute: {() -> Void in
  96. let url = URL(string: UIApplication.openSettingsURLString)
  97. if let url = url, UIApplication.shared.canOpenURL(url) { if #available(iOS 10.0, *) {
  98. UIApplication.shared.open(url, options: [:],completionHandler: {(success) in})
  99. }else {
  100. UIApplication.shared.openURL(url)
  101. }
  102. }
  103. })
  104. })
  105. }
  106. }