Check for Internet in Corona SDK

Checking for Internet before accessing the Internet data is right way to do in any app that you make in any platform.

Here is a small gist to make the network check on Corona SDK. Either use the code in the same lua file you use, or use a seperate utility lua file to add the functions. Check here for making the Utility file in Corona SDK (Lua).

Network Listener and network check using Sockets

Usage :

In the function where you want to check for the utility, just call the function, which will return you the network status.

Usage of Network function

Code Explanation :

1 — create a local variable.

2 — Create a network listener function available in the network library. Note that this will work only on the iOS, so we are using sockets for Android.

3 — we are checking for the Platform and if its iOS, we are sending the status using the network listener which automatically changes the value of the isNetworkAvailable variable.

4 — Using the Sockets to check the internet connection if the platform is Android.

5 — Based on the Socket test result, we are setting the value for the isNetworkAvailable variable.

6 — Close the test connection

7 — return the isNetworkAvailable Variable.

Thats for the Network. Happy coding !