4

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?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
deadcoder0904
  • 7,232
  • 12
  • 66
  • 163
  • Using the answer to this question: https://stackoverflow.com/questions/26971240/how-do-i-run-an-terminal-command-in-a-swift-script-e-g-xcodebuild, you could run `ping 8.8.8.8`, for example, and see if you get an answer. Getting an answer would mean there is Internet. Just in case Reachability does not offer a better way. – regina_fallangi Sep 25 '18 at 15:42
  • Oops why didn't I think of that. Thanks @regina_fallangi I'll update my code to see if it works – deadcoder0904 Sep 25 '18 at 15:46
  • It'd work everywhere but China, I believe. Do answer your own question if it works, it's definitely interesting to see if it works. – regina_fallangi Sep 25 '18 at 15:50
  • Yes true. I read some Apple website will work in China as well. Finding that answer so I can use that website instead of Google & sure I will answer the question. I always do :) – deadcoder0904 Sep 25 '18 at 15:51
  • take a look at https://stackoverflow.com/questions/7938650/ios-detect-3g-or-wifi – dniswhite Sep 25 '18 at 15:52
  • 1
    @dniswhite I have the same code in swift. its just there is no case of Wifi Connected but no internet :( – deadcoder0904 Sep 25 '18 at 15:53
  • @dniswhite that question is only to see if there is connection to the wifi and the solution is `Reachability`, which OP is already using. Please read the question better. – regina_fallangi Sep 25 '18 at 15:54
  • 2
    https://stackoverflow.com/questions/24713449/using-apples-reachability-to-check-remote-server-reachability-in-swift – Augie Sep 25 '18 at 15:55
  • Thanks @Augie so I think I can't use Reachability for that. I have to actually ping the server to see if the server is online :) – deadcoder0904 Sep 25 '18 at 15:59

1 Answers1

0

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

deadcoder0904
  • 7,232
  • 12
  • 66
  • 163