'Check for internet connectivity from Unity

I have a Unity project which I build for Android and iOS platforms. I want to check for internet connectivity on Desktop, Android, and iOS devices. I've read about three different solutions:

  1. Ping something (for example Google) - I totally dislike such decision, and I've read about mistakes on Android.

  2. Application.internetReachability - According to Unity's documentation, this function will only determine that I have a POSSIBILITY of connecting to the Internet (it doesn't guarantee a real connection).

  3. Network.TestConnection() - If I have no Internet connection, my application fails. So this isn't correct either.

How can I determine whether I have internet connectivity from within Unity?



Solution 1:[1]

I don't actually believe that Network.TestConnection() is the right tool for this job. According to the documentation, it looks to me like it's meant for testing if NAT is working and your client is publicly reachable by IP, but what you want to check for is whether you have general internet connectivity.

Here is a solution that I found on Unity Answers by user pixel_fiend, which simply tests a website to see if the user has connectivity. 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:

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
     }));
 }

You can change the website to whatever you want, or even modify the code to return success if any one of a number of sites are reachable. AFAIK 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.

Solution 2:[2]

public static IEnumerator CheckInternetConnection(Action<bool> syncResult)
{
    const string echoServer = "http://google.com";

    bool result;
    using (var request = UnityWebRequest.Head(echoServer))
    {
        request.timeout = 5;
        yield return request.SendWebRequest();
        result = !request.isNetworkError && !request.isHttpError && request.responseCode == 200;
    }
    syncResult(result);
}

Solution 3:[3]

void Start()
{
    StartCoroutine(CheckInternetConnection(isConnected =>
    {
        if (isConnected)
        {
            Debug.Log("Internet Available!");
        }
        else
        {
            Debug.Log("Internet Not Available");
        }
    }));
}

IEnumerator CheckInternetConnection(Action<bool> action)
{
    UnityWebRequest request = new UnityWebRequest("http://google.com");
    yield return request.SendWebRequest();
    if (request.error != null) {
        Debug.Log ("Error");
        action (false);
    } else{
        Debug.Log ("Success");
        action (true);
    }
}

Since WWW has deprecated, UnityWebRequest can be used instead.

Solution 4:[4]

i know this is old, but maybe can help you.

public bool CheckInternetConnection(){
    return !(Application.internetReachability == NetworkReachability.NotReachable)
}

Solution 5:[5]

I've created an Android's library that can be used as Unity's plugin for this purpose. If anyone's interested it's available under https://github.com/rixment/awu-plugin

As for the iOS version unfortunately I don't have enough of knowledge in this area. It would be nice if someone could extend the repo to include the iOS version too.

Solution 6:[6]

Ping 8.8.8.8 instead of sending a GET unity already provides the tool for that:

Ping

The ping operation is asynchronous and a ping object can be polled for status using Ping.isDone. When a response is received it is in Ping.time.

Windows Store Apps: A stream socket is used to mimic ping functionality, it will try to open a connection to specified IP address using port 80. For this to work correctly, InternetClient capability must be enabled in Package.appxmanifest.

Android: ICMP sockets are used for ping operation if they're available, otherwise Unity spawns a child process /system/bin/ping for ping operations. To check if ICMP sockets are available, you need to read the contents for /proc/sys/net/ipv4/ping_group_range. If ICMP sockets are available, this file should contain an entry for 0 2147483647.

Ping ping = new Ping("8.8.8.8");//sends request
while (!ping.IsDone) yield return new WaitForSeconds(1);//waits a second before next request
//is connected

Solution 7:[7]

You can check network connection using this code

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maximillian Laumeister
Solution 2 Nimantha
Solution 3 Arkar Min Tun
Solution 4 kamiyar
Solution 5
Solution 6 Alireza Jamali
Solution 7 Codemaker