在这里,我想添加观察者来检查我是否有互联网连接,
我使用了https://github.com/ashleymills/Reachability.swift的reachability-swift 5.0 ,我使用了这段代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let reachability = try! Reachability()
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability?.startNotifier()
print("start notifierrrr")
}catch{
print("could not start reachability notifier")
}
...
}
当我打开/关闭我的 wi-fi 时,该函数根本没有调用
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.currentReachabilityStatus {
case .reachableViaWiFi:
print("Reachable via WiFi bbb")
case .reachableViaWWAN:
print("Reachable via WWAN bbb")
case .notReachable:
print("Network not reachable bbb")
}
}
注意:我正在使用真实设备 iphone 7 并使用 ReachabilitySwift (5.0.0)