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:

-- 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  

So I shut off my iPad’s WiFi and expectedly get the fail message. That kinda works. It didnt know if I had a network connection or not, just that the request failed somehow.

Any buddy have any better way to check for a network connection?
[import]uid: 6114 topic_id: 4386 reply_id: 304386[/import]

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.

[lua]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[/lua] [import]uid: 11393 topic_id: 4386 reply_id: 13978[/import]

it is on daily build

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

look for network reachability

c. [import]uid: 24 topic_id: 4386 reply_id: 38833[/import]

Network reachability available for Android yet? [import]uid: 18783 topic_id: 4386 reply_id: 39339[/import]

not yet. it is on the to fix bugs

c [import]uid: 24 topic_id: 4386 reply_id: 39350[/import]

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) [import]uid: 18783 topic_id: 4386 reply_id: 39356[/import]