I use an viewController which inherit BaseViewController. The function "monitorNetworkStatus()" is invoked in BaseViewController's method "viewDidLoad".
private func monitorNetworkStatus() {
ReachabilityManager.shared.startMonitoring { [weak self] (status, presentingVC) in
print(self?.description)
}
}
ReachabilityManager is a singleton。The startMonitoring function like this
func startMonitoring(reachabilityStatus: @escaping (_ status: AFNetworkReachabilityStatus, _ presentingVC: UIViewController?) -> Void) {
AFNetworkReachabilityManager.shared().setReachabilityStatusChange { [weak self] (status) in
if status != self?.networkStatus {
// Only notify when status toggling between reachable and not reachable
if (self?.networkStatus == .notReachable &&
(status == .reachableViaWiFi || status == .reachableViaWWAN)) ||
status == .notReachable {
reachabilityStatus(status, self?.getPresentingViewController())
}
self?.networkStatus = status
}
}
AFNetworkReachabilityManager.shared().startMonitoring()
When net status changes it will print nil occasionally.