1

Iam using c# for my application in unity.At some methods I need to check for internet connection for that I am using Application.internetReachability == NetworkReachability.NotReachable this line of code working properly while i am running in unity editor but when I checked for mac build its not working properly.

public static bool CheckInternetConnectionAvailability()
{
    try
    {
        return !(Application.internetReachability == NetworkReachability.NotReachable);
    }
    catch(Exception ex)
    {
        LoggingManager.Error(ex);

        return true;
    }
}

When there is no internet is available it is returning true but it has to be true.Is there anything that iam doing wrong?

Deepak
  • 545
  • 1
  • 12
  • 36

1 Answers1

1

The Application.internetReachability property returns the type of Internet reachability currently possible on the device. You shouldn't be using that to check for internet connection.

Here is a warning and the reason from the doc:

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.

To check for internet availability, use the WWW, UnityWebRequest or HttpWebRequest API to write a simple function that makes a request to a remote server. If no error then the the device is connected to the internet. I suggest you use HttpWebRequest over WWW and UnityWebRequest because they seem to freeze when there is no internet connection. Hopefully, that bug has been fixed.

See this post for example on how to check for internet connection with HttpWebRequest.

Programmer
  • 121,791
  • 22
  • 236
  • 328