Is it possible to check if WiFi is connected but there is no internet connection in Swift using Reachability?

2k views Asked by At

I don't know if this is possible but here's my question -

  • When the WiFi is connected & the internet is ON then Yellow Light glows on Router
  • When the WiFi is connected & the Internet is OFF then Red Light glows on Router

So is it possible to distinguish the 2nd case or is it just the same as when Internet is not reachable?

I am using https://github.com/ashleymills/Reachability.swift

Complete code can be found https://github.com/deadcoder0904/net-alert

Its basically in just one file

Relevant code -

let reachability = Reachability()!

reachability.whenReachable = { reachability in
    if reachability.connection == .wifi {
            print("Reachable via WiFi")
            self.setStatusIcon(color: "green")
    } else {
            print("Reachable via Cellular")
            self.setStatusIcon(color: "yellow")
    }
}

reachability.whenUnreachable = { _ in
    print("Not reachable")
    self.setStatusIcon(color: "red")
}

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

where setStatusIcon() sets the status icon according to the color

I want to know when internet is not reachable but WiFi is connected in Swift?

1

There are 1 answers

0
deadcoder0904 On BEST ANSWER

I found a solution.

As Augie suggested, its not possible to do it with Reachability as it just checks if the WiFi or Cellular is reachable or not.

So I had to ping a website to check if it is online or not.

If I get a 200 response from it then its connected.

But if I don't get a 200, then even though WiFi is connected but there is no internet.

I didn't know how to Ping properly so I asked another question where I got the answer

The complete code can be found at https://github.com/deadcoder0904/net-alert