2) How to use
For common scenarios such as UIViewController
, follow the CCNetworkStatusProtocol
protocol.
Similarly, in `class` such as UIView
, AppDelegate
, etc., follow the CCNetworkStatusProtocol
protocol.
Through the return value of the isReachable
method, you can verify the judgment.
class CCViewController: UIViewController, CCNetworkStatusProtocol {
if isReachable() {
print("Network available")
} else {
print("Network unavailable")
}
}
/// Network status protocol
protocol CCNetworkStatusProtocol {
func isReachable() -> Bool
}
extension CCNetworkStatusProtocol {
/// Return a boolean value for real-time monitoring of the network status
func isReachable() -> Bool {
var res: Bool = false
let netManager = NetworkReachabilityManager()
if netManager?.status == .reachable(.ethernetOrWiFi) || netManager?.status == .reachable(.cellular) {res = true}
return res
}
}