How to check internet connection in an app

Hello all.
I just a newbie in the programming Unity. And I here have a trouble.

I have an app, and I want this app when running in Android/iOS platform, this app is connected to internet. If the internet connection is off, this app show a dialogue box : “Please connect to internet” & disabling all features or cannot run, until it connected to internet.

Please help me to create the C# script in unity. And where I place this script? in gameObject or else…?

Thank you very much.

4 Likes

Check unity answers: http://answers.unity3d.com/answers/744803/view.html

8 Likes
 if(Application.internetReachability == NetworkReachability.NotReachable)
        {
            Debug.Log("Error. Check internet connection!");
        }
51 Likes

Best answer , clean and easy.

10 Likes

And wrong. You don’t use this to check if you actually can reach the internet, but to distinguish between WiFi and Carrier networks.

22 Likes

I would just make a simple WWW call to some webpage. If it returns success, you have internet, if it returns an error, you don’t.

5 Likes

If you need 100% reachability check, it’s not a matter of the user being connected to “a” network. It’s a matter of the user being able to reach a certain service that you should carefully pick.

Personally, I opt-in for a simple endpoint on my servers that the users try to reach, and if that succeeds, I can guarantee they can use my game services.

One of the many cases they might be connected to a network but can’t reach your services are captive networks and intranets.

9 Likes

This does not seem quite right. Application.internetReachability returns a NetworkReachability enum, which has three values: WiFi/Wired, Cellular, and None. So you can use this to detect say “you have no carrier and no wifi”.

The other comments here that you probably want to also ping some low-cost endpoint on whatever server/s you’re interested in still hold true, for the reasons other folks have mentioned.

2 Likes

I’d verify you can actually connect to your specific game service rather than a generic internet connection check. Users may be attached to a public wifi that requires a webpage sign in, where some services are allowed without sign in but others are blocked. Firewalls on public or private corporate wifi may block certain ports that may be important to you, but let through traffic you would use for a generic internet check. Really only checking the connection to your actual game service is the closest thing to a guarantee.

5 Likes

I agree. You can definitely test whether network is present with Application.internetReachability. Additionally pinging endpoint will verify data transfer capability.

1 Like

This is the information from:

You can be connected to the hot spot but the hotspot itself has no internet access.

“Note: Do not use this property to determine the actual connectivity. E.g. the device can be connected to a hot spot, but not have the actual route to the network. Non-handhelds are considered to always be capable of NetworkReachability.ReachableViaLocalAreaNetwork.”

LoOnar Forge

6 Likes

How would you interpret the case where Application.internetReachability returns NetworkReachability.NotReachable?

2 Likes

Years later but…Sounds cool. In my case im using Photon, so i will go to check the game has… data transfer capability.

if (!PhotonNetwork.connected)
{
// do something
}

1 Like

If your game/app uses Google Play Games you can simply pause at the splash screen and wait for a google signin to continue playing.

1 Like

Just ping google.com, since it’s pretty much guaranteed to be up :slight_smile:

You’ll get false negative results on corporate networks which sometimes are configured to block ICMP packets from passing through the firewall. The network I wrote this message on for example.

1 Like

I think some people are misreading the documentation. They tell you not to use to check internet availability IF the devices are handheld. If not, this works perfect since the wifi/lan internet option is true or false when NetworkReachability.ReachableViaLocalAreaNetwork. is checked. I think it would work still on handhelds, since the hotspot has to create some sort of network using the tethered device as a router. Maybe the refer to the innacuracy of the class to properly detect if there is internet feed on a handheld connected to a hotspot network. In any case, if the connection is strong enough, it will get detected, if not, although present for email or other apps, don’t think would be of much use for multiplayer connectivity though, so it’s a moot point if the NetworkReachability class can accurate see if a handheld is receiving internet or not.

Thanks!

This was perfect for my scenario, where I simply wanted to detect that the users phone was in airplane mode so I could just disable all the networking altogether.

As people have pointed out you still need to check connectivity when online ,but this is very useful to know with absolute certainty that you don’t even need to try looking for connectivity because you’e offline.

1 Like

If you don’t have your own Ressource to check against, you can Ping as a fallback 8.8.8.8 which is googles server and unless your user is behind a state run firewall or a zombie outbreak, it’s safe bet to determine for internet availability.

2 Likes

what if I want my app to work during a zombie outbreak in china :frowning:

3 Likes