1

When I start my game, I try to load a rewarded video ad from admob. My problem is, that I need to check if the user has internet connection established, in order to take the according actions, mainly to show/hide Game Objects and Texts. I am using this code in the Start() method:

if (Application.internetReachability != NetworkReachability.NotReachable)

But it's not always reliable, and many times the statement will be false, even though I am connected via Wi-Fi. After a few seconds though, connection is established and I can proceed, but I would like to find a way to avoid this problem. So is there a better way to check for the connection, maybe even asynchronously, like checking while loading the main menu?

James Ele
  • 111
  • 3
  • 13
  • See if this helps https://answers.unity.com/questions/567497/how-to-100-check-internet-availability.html?childToView=744803#answer-744803 – Edney Holder Sep 13 '19 at 19:17
  • 1
    Possible duplicate of [Unity check internet connection availability](https://stackoverflow.com/questions/24351155/unity-check-internet-connection-availability) – derHugo Sep 13 '19 at 19:19
  • [`Application.internetReachability`](https://docs.unity3d.com/ScriptReference/Application-internetReachability.html) `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.` – derHugo Sep 13 '19 at 19:20

1 Answers1

1

Application.internetReachability should not be used to determine the actual connectivity according to unity documentation!.

Here is a simple method that may work for you:

 IEnumerator checkInternetConnection(Action<bool> action){
     WWW www = new WWW("http://google.com");
     yield return www;
     if (www.error != null) {
         action (false);
     } else {
         action (true);
     }
 } 
 void Start(){
     StartCoroutine(checkInternetConnection((isConnected)=>{
         // handle connection status here
     }));
 }

This method simply tests a website to see if the user has connectivity.(I use google because it's always up).One benefit of this code is that it uses IEnumerator for asynchronous operation, so the connectivity test won't hold up the rest of your application. There is no way to check for true internet connectivity without trying to connect to a specific site on the internet, so "pinging" one or more websites like this is likely to be your best bet at determining connectivity.

However,I wouldn't suggest using this code for commercial applications with a lot of users because using the google webpage for an internet reachability check might get you in trouble.Such a usecase usually counts as misuse of their services. There isn't a problem using this method for a small app but if your game / application gets successful and is played by 10k / 100k or even millions of people you generate a lot traffic on the google servers.

Best approach is to use your own server which returns something you can identify. Since there might be caches implemented in your route, the simplest way is to build an echo server. So you send a random string / number to your server and it simply sends it back as content.

user601
  • 104
  • 6