Check Network Connection with Callback function table

Posted by marble68, Posted on August 29, 2011

2 votes

My app has some social elements to it. Primarily, posting to Facebook and tweeting. When the user touches either action, I first wanted to check their Internet connection and wait on the result. Thanks Lerg for help.

To use, you pass several arguments:
The URL to check, and four function callbacks for unreachable, reachable via Wifi, via Cell, and viaOther.

The calling code looks like this for example:

1
DataNet.CheckURL("twitter.com",CheckConnection,doTweet,requireOK2Tweet,requireOK2Tweet)

Depending on Scope, you can refer back to the table with things like:

1
2
3
4
if DataNet.bReachable==true then
 
print ( DataNet.sHostURL .. " is reachable via " .. DataNet.reachableStatus[DataNet.iReachableState])
end

This is the actual table. It needs a timeout capability which I hope to add in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
DataNet = {
  CheckURL = function(sUrl, funcNotReachable, funcWifi, funcCell, funcOther)
    DataNet.iReachableState= 0
    DataNet.sHostURL = sUrl
 
    DoCheck = function()
      if network.canDetectNetworkStatusChanges then
            network.setStatusListener( DataNet.sHostURL, NetListener )
      end
    end
 
    NetListener = function(event)
        network.setStatusListener( DataNet.sHostURL, nil )
        DataNet.bReachable = event.isReachable
        if not DataNet.bReachable==true then
          funcNotReachable()
        elseif event.isReachableViaWiFi then
          DataNet.iReachableState = 3
          funcWifi()
        elseif event.isReachableViaCellular then
          DataNet.iReachableState = 2
          funcCell()
        else
          DataNet.iReachableState = 1
          funcOther()
        end
 
 
 
    end
    DoCheck()
 
  end,
    bReachable=false,
    iReachableState=0,
    reachableStatus = {"other","cell","wifi"},
    sHostURL = ""
}

usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function CheckConnection()
     local sUrl = values.addHttp(values.DataNet.sHostURL)
    local function CheckConnectionComplete( event )
        if "clicked" == event.action then
                local i = event.index
                if 1 == i then
                        return true
                elseif 2 == i then
                       system.openURL( sUrl )
                end
        end
    end
 
    local alert = native.showAlert( "No Internet Connection or Service unreachable", "No connection to " .. sUrl ..". That service is unreachable and could be down or you are not conencted to the Internet. An Internet connection is required to complete this. Please make sure you are connected!",{ "OK","Test" }, CheckConnectionComplete )
 
end
 
function requireOK2Tweet()
    local function requireOK2TweetonComplete( event )
        if "clicked" == event.action then
                local i = event.index
                if 1 == i then
                        doTweet()
                elseif 2 == i then
                        return true
                end
        end
    end
 
    local alert = native.showAlert( "Not Wifi!", "You are not using a WiFi connection. There may be charges to do this. Are you sure?",{ "Yes", "No" }, requireOK2TweetonComplete )
 
end
 
function function doTweet()
 -- Put your tweet functions here - See sample code for twitter example
end
 
DataNet.CheckURL("twitter.com",CheckConnection,doTweet,requireOK2Tweet,requireOK2Tweet)


Replies

canupa.com's picture
canupa.com
User offline. Last seen 20 hours 36 min ago. Offline
Joined: 20 Jun 2011

very nice! will check that out!

-finefin