Check For Internet Connection

Hello! Is there a way to check for an active internet connection? I want to do the check when the app launches.

I created a scene in storyboard and used the code I found here: 

http://developer.coronalabs.com/code/check-internet-connection

scene1.lua

local http = require("socket.http") local ltn12 = require("ltn12") if http.request( "http://www.google.com" ) == nil then         local function onCloseApp( event )         if "clicked" == event.action then                 os.exit()         end         end         native.showAlert( "Alert", "An internet connection is required to use this application.", { "Exit" }, onCloseApp ) else     storyboard.gotoScene( "scene2", "crossFade", 75  ) end

It works pretty well but slows the application. There is a huge delay between scenes and Revmob fullscreen ad no longer shows.

Is there a better way to check for an active internet connection?

Thank you!

Hi there,

On iOS, you can use the network.setStatusListener() API, which will automatically monitor the network status.  Check it out here: http://docs.coronalabs.com/api/library/network/setStatusListener.html.

On Android, I do something similar to what you do – a quick socket test to see if the internet is accessible or not.

Hope this helps!

  • Andrew

Hello aukStudios and thanks for the quick response.

The problem is actually on Android; on a slow device this test might freeze the app for a couple of seconds or even crash it.

Hi there,

Try using something like the following instead.  You can set the timeout of the socket connection, so that it won’t hold up your app indefinitely.

You can wrap this code into a function which will return true/false whether the internet connection is available per this method of testing it.

[lua]

local socket = require(“socket”)

            

local test = socket.tcp()

test:settimeout(1000)                   – Set timeout to 1 second

            

local testResult = test:connect(“www.google.com”, 80)        – Note that the test does not work if we put http:// in front

if not(testResult == nil) then

    print(“Internet access is available”)

else

    print(“Internet access is not available”)

end

            

test:close()

test = nil

[/lua]

Andrew,

Thank you for the awesome post. It works very fast and doesn’t slow down the app.

I combined your code with the first one and tested it on my device.

You should share it  as there are many developers having a hard time figuring out how to make a fast check on a internet connection ( http://developer.coronalabs.com/code/check-internet-connection), and your method is very good.

Sorry for my bad english.

Stefan

Andrew @aukStudios, I like the timeout in your code.  I didn’t have on in mine.  Thank you for sharing!

Naomi

Hmmm… actually, updating my code caused some conflict (that is, my app started behaving oddly), so I reverted mine back to this:

[lua]

local function networkConnection()

    local netConn = require(‘socket’).connect(‘www.google.com’, 80)

    if netConn == nil then

        return false

    end

    netConn:close()

    return true

end

[/lua]

I wonder why this revised version didn’t work for me:

[lua]

local function networkConnection()

    local socket = require(“socket”)

    local test = socket.tcp()

    test:settimeout(1000)  – Set timeout to 1 second

    local netConn = test:connect(“www.google.com”, 80)

    if netConn == nil then

        return false

    end

    netConn:close()

    return true

end

[/lua]

Naomi

Hi Naomi,

In the revised version, I think line 10 should be test:close() rather than netConn:close().  That might explain the odd behavior you said you were seeing.

  • Andrew

Ah, Andrew, thank you for spotting it.  Appreciate it!

Naomi

No problem!

Hi there,

On iOS, you can use the network.setStatusListener() API, which will automatically monitor the network status.  Check it out here: http://docs.coronalabs.com/api/library/network/setStatusListener.html.

On Android, I do something similar to what you do – a quick socket test to see if the internet is accessible or not.

Hope this helps!

  • Andrew

Hello aukStudios and thanks for the quick response.

The problem is actually on Android; on a slow device this test might freeze the app for a couple of seconds or even crash it.

Hi there,

Try using something like the following instead.  You can set the timeout of the socket connection, so that it won’t hold up your app indefinitely.

You can wrap this code into a function which will return true/false whether the internet connection is available per this method of testing it.

[lua]

local socket = require(“socket”)

            

local test = socket.tcp()

test:settimeout(1000)                   – Set timeout to 1 second

            

local testResult = test:connect(“www.google.com”, 80)        – Note that the test does not work if we put http:// in front

if not(testResult == nil) then

    print(“Internet access is available”)

else

    print(“Internet access is not available”)

end

            

test:close()

test = nil

[/lua]

Andrew,

Thank you for the awesome post. It works very fast and doesn’t slow down the app.

I combined your code with the first one and tested it on my device.

You should share it  as there are many developers having a hard time figuring out how to make a fast check on a internet connection ( http://developer.coronalabs.com/code/check-internet-connection), and your method is very good.

Sorry for my bad english.

Stefan

Andrew @aukStudios, I like the timeout in your code.  I didn’t have on in mine.  Thank you for sharing!

Naomi

Hmmm… actually, updating my code caused some conflict (that is, my app started behaving oddly), so I reverted mine back to this:

[lua]

local function networkConnection()

    local netConn = require(‘socket’).connect(‘www.google.com’, 80)

    if netConn == nil then

        return false

    end

    netConn:close()

    return true

end

[/lua]

I wonder why this revised version didn’t work for me:

[lua]

local function networkConnection()

    local socket = require(“socket”)

    local test = socket.tcp()

    test:settimeout(1000)  – Set timeout to 1 second

    local netConn = test:connect(“www.google.com”, 80)

    if netConn == nil then

        return false

    end

    netConn:close()

    return true

end

[/lua]

Naomi

Hi Naomi,

In the revised version, I think line 10 should be test:close() rather than netConn:close().  That might explain the odd behavior you said you were seeing.

  • Andrew

Ah, Andrew, thank you for spotting it.  Appreciate it!

Naomi

No problem!

For anyone using the socket method: http://w3.impa.br/~diego/software/luasocket/tcp.html#settimeout

I had some serious problems, the settimeout uses seconds not miliseconds  :slight_smile:

Use:

settimeout(1) -- sets timeout to 1 second