ReachabilitySwift 有什么问题?

What is the issue with ReachabilitySwift?

在我的 viewDidLoad() 函数中,我调用了这个方法:

//MARK: Reachability
func startReachability(){
    //declare this property where it won't go out of scope relative to your listener
    do{
        let reachability = try Reachability.reachabilityForInternetConnection()

        reachability.whenReachable = { reachability in
            // this is called on a background thread, but UI updates must
            // be on the main thread, like this:
            dispatch_async(dispatch_get_main_queue()) {
                if reachability.isReachableViaWiFi() {
                    print("Reachable via WiFi")
                } else {
                    print("Reachable via Cellular")
                }
            }
        }
        reachability.whenUnreachable = { reachability in
            // this is called on a background thread, but UI updates must
            // be on the main thread, like this:
            dispatch_async(dispatch_get_main_queue()) {
                print("Not reachable")
            }
        }

        try reachability.startNotifier()

    } catch {
        print("Unable to start notifier")
    }
}

但是不行。它只调用 whenReachable()whenUnreachable() 一次,当我关闭和打开 Wi-Fi 时,它什么也不做。

仅当您直接调用它时它才会验证您的网络method.If您希望收到有关网络丰富性更改的通知您应该使用丰富性通知。

观察者通知。

//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!

//declare this inside of viewWillAppear

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:",name: ReachabilityChangedNotification,object: reachability)
do{
  try reachability.startNotifier()
}catch{
  print("could not start reachability notifier")
}

处理更改:

func reachabilityChanged(note: NSNotification) {

  let reachability = note.object as! Reachability

  if reachability.isReachable() {
    if reachability.isReachableViaWiFi() {
      print("Reachable via WiFi")
    } else {
      print("Reachable via Cellular")
    }
  } else {
    print("Network not reachable")
  }
}

取消订阅:

reachability.stopNotifier()
NSNotificationCenter.defaultCenter().removeObserver(self,
                                                    name: ReachabilityChangedNotification,
                                                    object: reachability)

我需要对 Reachability 实例的强烈引用!所以我应该在 class 级别声明它。

在 Swift 4 中,我遇到了 whenReachable clouser 未执行的问题。 我发现所有通知都应该在主队列中传递。

使用下面的代码解决

//在文件顶部声明此 属性,因此范围将保留。

let reachability = Reachability()!

 func setUpReachability()
    {
        //declare this property where it won't go out of scope relative to your listener
        DispatchQueue.main.async {

            self.reachability.whenReachable = { reachability in
            if reachability.connection == .wifi {
                print("Reachable via WiFi")
            } else {
                print("Reachable via Cellular")
            }
        }
            self.self.reachability.whenUnreachable = { _ in
            print("Not reachable")
        }

        do {
            try self.reachability.startNotifier()
        } catch {
            print("Unable to start notifier")
        }

        }
    }