Check Unity Internet Connection

I have a Unity project that I am creating for the Android and iOS platforms. I want to check my Internet connection on Desktop, Android and iOS devices. I read about three different solutions:

  • Ping something (like Google) - I totally dislike this solution, and I read about errors on Android.

  • Application.internetReachability - According to the Unity documentation, this function will only determine that I have the OPPORTUNITY to connect to the Internet (this does not guarantee a real connection).

  • Network.TestConnection() - If I do not have an Internet connection, my application does not work. So this is not true.

How to determine if I have an internet connection inside Unity?

+11
source share
5 answers

In fact, I do not believe Network.TestConnection() is a suitable tool for this work. According to the documentation , it seems to me that it is designed to test the health of NAT and the availability of your client by IP, but you want to check if you have a common Internet connection.

Here is the solution I found in Unity pixel_fiend user answers that just tests the website to determine if the user has a connection. One of the advantages of this code is that it uses IEnumerator for asynchronous operation, so the connection test will not delay 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 any other or change the code so that it returns success if any of several sites is available. In fact, there is no way to verify the authenticity of an Internet connection without trying to connect to a specific site on the Internet, so pinging one or more websites like this is probably the best choice for determining an Internet connection.

+10
source

I know this is old, but maybe it can help you.

 public bool CheckInternetConnection(){ return !(Application.internetReachability == NetworkReachability.NotReachable) } 
+3
source
  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); } 
+2
source

The right way to check for a β€œguaranteed” Internet connection is to discover a portable portal using an HTTP request to a place with known content. This is how the operating systems themselves check whether they should provide the user with a wifi login screen.

Just ping or checking the error / success status of a www request is not enough, for example. Hotel / airport wifi login page can be returned without error code (instead of the desired page).

Additionally, for example, on iOS there are new additional restrictions (application transport security, which allows the use of http only with explicit white resolution).

My asset is a simple solution, although not free: Checking the availability of the Internet in the asset store.

+1
source

You can try the following:

  if(Application.internetReachability == NetworkReachability.NotReachable) { // There is no internet connection Debug.Log("Error. Check internet connection!"); } else {// there is internet connection , do stuff here} 
0
source

Source: https://habr.com/ru/post/1237588/


All Articles