Swift:我怎样才能有一个监听器报告何时连接丢失以及何时返回?

lmi*_*asf 5 reachability ios swift swift2 swift3

我想在我的iOS应用程序启动时调用一个方法,但我只想在有连接时调用此方法.我发现在Objective-C中你可以使用Reachable,但事实证明这个方法不是Swift的一部分.

我找到了一个名为Reachability.swift的pod ,我正在使用提供的示例:

override func viewWillAppear(animated: Bool) {
    let reachability: Reachability
    do {
        reachability = try Reachability.reachabilityForInternetConnection()
    } catch {
        print("Unable to create Reachability")
        return
    }
    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: "reachabilityChanged:",
        name: ReachabilityChangedNotification,
        object: reachability)

    do {
        try reachability.startNotifier()
    } catch {
        print("This is not working.")
            return
    }
}

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("Not reachable")
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这不正常.我只在我进入ViewController时工作,但是当我打开并关闭WiFi时不工作.

lmi*_*asf 11

我通过声明reachability为ViewController的实例变量来解决这个问题:

var reachability: Reachability!
Run Code Online (Sandbox Code Playgroud)

因此应该从方法中删除此变量viewWillAppear.

斯威夫特2

override func viewWillAppear(animated: Bool) {
    do {
        reachability = try Reachability.reachabilityForInternetConnection()
    } catch {
        print("Unable to create Reachability")
        return
    }

    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: "reachabilityChanged:",
        name: ReachabilityChangedNotification,
        object: reachability)

    do {
        try reachability.startNotifier()
    } catch {
        print("This is not working.")
        return
    }

}

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("Not reachable")
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift 3(由Burning提供)

var reachability: Reachability!
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    reachability = Reachability()
    NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(_:)), name: Notification.Name.reachabilityChanged, object: reachability)

    do {
        try reachability?.startNotifier()
    } catch {
        print("This is not working.")
        return
    }

}

func reachabilityChanged(_ note: NSNotification) {

    let reachability = note.object as! Reachability

    if reachability.connection != .none {
        if reachability.connection == .wifi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    } else {
        print("Not reachable")
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作正常:).