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?

9
  • 1
    Oops why didn't I think of that. Thanks @regina_fallangi I'll update my code to see if it works Commented Sep 25, 2018 at 15:46
  • 1
    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 :) Commented Sep 25, 2018 at 15:51
  • 2
    @dniswhite I have the same code in swift. its just there is no case of Wifi Connected but no internet :( Commented Sep 25, 2018 at 15:53
  • 2
    stackoverflow.com/questions/24713449/… Commented Sep 25, 2018 at 15:55
  • 1
    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 :) Commented Sep 25, 2018 at 15:59

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.