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

1

5

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.

Glennaglennie answered 23/11, 2015 at 19:17 Comment(0)
G
11

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 :).

Glennaglennie answered 23/11, 2015 at 19:22 Comment(1)
Hey I tried to use this for my project, but it seems that in swift 3 .reachabilityForInternetConnection() is not recognised: can you help me how to fix it?Genetic

© 2022 - 2024 — McMap. All rights reserved.