Apple wants us to check for Network Reachability - or else.

In my research on iOS App submittal I noticed this:

From: http://developer.apple.com/news/ios/appstoretips/

Don't Forget to Include Network Error Alerts in Your Code

If your application provides functionality that requires access to a network, it's very important that your code include a customer alert or notification when the network is not available.

The Reachability sample application demonstrates how to use the System Configuration Reachability API to monitor the network state of an iPhone or iPod touch. Use this sample code to learn how to detect the absence of Wi-Fi and Wireless Wide Area Network (WWAN) services so your application knows when it's necessary to produce a network error alert.

Your users will appreciate knowing when an application has no network access — and missing "network alerts" is the third most common reason for applications being returned to developers for modification.

Now I did find some code on the Lua site that works fine, to check for the existence of a web file by getting its header, Apache style, as:

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
-- load the http module
local http = require("socket.http")
 
local function checkNetwork( passURL )
-- Requests information about a document, without downloading it.
-- resultR is 1, resultC is 200, and resultH would return the following headers:
-- resultCH = {
--   [connection] => "close"
--   [content-type] => "text/html"
--   [vary] => "Accept-Encoding"
--   [date] => "Fri, 10 Dec 2010 23:42:39 GMT"
--   [x-powered-by] => "PHP/5.2.14"
--   [server] => "Apache"
-- }
 
    local resultR, resultC, resultH
    resultR, resultC, resultH = http.request {
        method = "HEAD",
        url = passURL
    }
 
-- resultC contains Apache style HTTP error codes, 200 for success, 404 for page not found, etc.
    if ( resultC == 200 ) then
        return true
    else
        AlertMsg = "An internet connection is required for this feature."
        native.showAlert( AlertTitle, AlertMsg, { "OK" } )        
        return false
    end
end

I use sockets to test if the target host is reachable before I attempt a HTTP connection. But again this only tells us if the target host is up, not whether or not there is a data connection to the device. But if your target host is down, it's as good as not having a data connection anyway.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local socket = require( "socket") -- Required library to test socket connection
 
local host = "www.google.com
local hostFound = true
local con = socket.tcp()
con:settimeout( 2 ) -- Timeout connection attempt after 2 seconds
                
-- Check if socket connection is open
if con:connect(host, 80) == nil then 
   hostFound = false
   print( "Host Not Found" )
else
   print( "Host Found" )
end

it is on daily build

http://developer.anscamobile.com/reference/index/network

look for network reachability

c.

Network reachability available for Android yet?

not yet. it is on the to fix bugs

c

Well the code above looked promising for handling both platforms in one place.

It worked in the simulator when I turned off my internet connection. The alert came right up.

Built for device and ran on device and the alert does not launch using this code when hostfound is false

local alert = native.showAlert("Not Connected to Internet", "Please connect to network and try again", { "OK" })

I know that Apple will nix anything I send with the majority of my menu items playing videos and pulling data from the internet if I can't test for it.

May have to test for 3G vs Wifi as well due to streaming data amounts. (another Apple rule)

views:1822 update:2011/9/30 9:15:39
corona forums © 2003-2011