Skip to content

Instantly share code, notes, and snippets.

@iranjith4
Last active June 13, 2018 17:03
Show Gist options
  • Save iranjith4/b1daa6caa68f34ac7820afc4ae4e7028 to your computer and use it in GitHub Desktop.
Save iranjith4/b1daa6caa68f34ac7820afc4ae4e7028 to your computer and use it in GitHub Desktop.
local iosNetworkAvailable = true -- 1
function networkListener( event )
iosNetworkAvailable = event.isReachable
end
if ( network.canDetectNetworkStatusChanges ) then
network.setStatusListener( "www.apple.com",networkListener ) -- 2
end
function isRechable()
local os = system.getInfo("platformName")
if ( os == "iPhone OS") then
return iosNetworkAvailable -- 3
elseif ( os == "Android" ) then
local socket = require("socket") -- 4
local test = socket.tcp()
local isNetworkAvailable = false
test:settimeout(3000) -- Set timeout to 3 seconds
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 -- 5
print("Internet access is available")
isNetworkAvailable = true
else
print("Internet access is not available")
isNetworkAvailable = false
end
test:close() -- 6
test = nil
return isNetworkAvailable -- 7
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment