Last active
June 13, 2018 17:03
-
-
Save iranjith4/b1daa6caa68f34ac7820afc4ae4e7028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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