Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: How can I have a listener that reports when connection is lost and when it comes back?

I would like to call a method when my iOS app starts, but I just want to call this method when there is connection. I have found that in Objective-C you can use Reachable, but it turns out that this method is not part of Swift.

I have found a pod called Reachability.swift, and I am using the example that was provided:

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")
    }
}

However, this is not properly working. I works only when the I enter that ViewController, but not when I turn on and turn off the WiFi.

like image 898
lmiguelvargasf Avatar asked Nov 23 '15 19:11

lmiguelvargasf


1 Answers

I have solve this issue by declaring reachability as an instance variable of the ViewController:

var reachability: Reachability!

So this variable should be removed from the method viewWillAppear.

Swift 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")
    }
}

Swift 3 (provided by 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")
    }
}

This worked properly :).

like image 110
lmiguelvargasf Avatar answered Sep 18 '22 11:09

lmiguelvargasf