我想连续检查有效的互联网连接。如果互联网可用,则应调用一些回调方法,或者如果关闭互联网,则不应调用回叫方法。请告诉我该怎么办?< / p>
我使用以下代码检查互联网是否可用?
NotificationCenter.default.addObserver(self, selector: #selector(checkForReachability), name: ReachabilityChangedNotification, object: reachability)
do {
try reachability?.startNotifier()
}
catch {
DILog.print(items: "Error Occured")
}
func checkForReachability(notification:NSNotification) {
let networkReachability = notification.object as! Reachability;
let remoteHostStatus = networkReachability.currentReachabilityStatus
if (remoteHostStatus == .notReachable) {
DILog.print(items: "Not Reachable")
}
else if (remoteHostStatus == .reachableViaWiFi) {
DILog.print(items: "Reachable")
}
}