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.