NetworkReachabilityManager
来自Alamofire
我遇到问题。我尝试使用此示例代码测试连接:
override func viewDidLoad() {
super.viewDidLoad()
let manager = NetworkReachabilityManager(host: "www.apple.com")
manager?.listener = { status in
switch status {
case .notReachable:
print("network connection status - lost")
case .reachable(NetworkReachabilityManager.ConnectionType.ethernetOrWiFi):
print("network connection status - ethernet/WiFI")
case .reachable(NetworkReachabilityManager.ConnectionType.wwan):
print("network connection status - wwan")
default:
break
}
}
manager?.startListening()
}
当我试图关闭wifi时,我只收到空白回复。没有诸如"网络连接状态之类的响应 - 丢失"。
但是当我试图打开wifi并且我收到了结果"网络连接状态 - ethernet / WiFI"其中反应良好。知道.notReachable
有什么问题吗?任何建议都表示赞赏。
答案 0 :(得分:2)
根据Alamofire
"确保记得留住经理"所以创建一个像这样的经理
class A: UIViewController{
let manager = NetworkReachabilityManager(host: "www.apple.com")
override func viewDidLoad() {
super.viewDidLoad()
// before start listening you can check
if (manager?.isReachableOnEthernetOrWiFi == true)
{
print("internet is available")
}
else
{
print("internet is not available")
}
manager?.startListening()
manager?.listener = { status in
switch status {
case .notReachable:
print("network connection status - lost")
case .reachable(NetworkReachabilityManager.ConnectionType.ethernetOrWiFi):
print("network connection status - ethernet/WiFI")
case .reachable(NetworkReachabilityManager.ConnectionType.wwan):
print("network connection status - wwan")
default:
break
}
}
}
}