1. pod install
use_frameworks!
pod 'ReachabilitySwift'
2. build error...
3. Just put the Reachability.swift file in my product.
// 网络状态监听
let reachability = Reachability()!
func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi");
if(mLoadWebViewStackSize == 0)
{
//reloadData();
}
case .cellular:
print("Reachable via Cellular");
if(mLoadWebViewStackSize == 0)
{
//reloadData();
}
case .none:
//alert_noNetwrok();
print("Network not reachable")
}
}
// 移除消息通知
deinit {
reachability.stopNotifier();
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability);
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
NotificationCenter.default.addObserver(
self,
selector: #selector(self.reachabilityChanged),
name: .reachabilityChanged,
object: reachability
)
do {
try reachability.startNotifier()
} catch {
print("could not start reachability notifier")
}
}