Checking Internet Connection

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
endernafi
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 296
Joined: Wed May 02, 2012 12:23 pm
Contact:

Checking Internet Connection

Post by endernafi »

Hi there dear LiveCoders,

At some certain occasions, my apps crash if there is no internet connection.
Here is my solution to check if there is connection.

Put a blank text file on a server, your website or any other secure location.
In my case, the file is only 13bytes.
Then put this code snippet into your script:

Code: Select all

function checkInternetConnection
  if the environment is "mobile" then
     put "http://www.example.com/checkConnection.txt" into theSource
     put specialFolderPath("documents") & "/checkConnection.txt" into theTarget
     libUrlDownloadToFile theSource, theTarget
     if ((there is a file theTarget) and (calcFileSize(specialFolderPath("documents"), "checkConnection.txt") > 0)) then
        delete file theTarget
        return true
     else
        return false
     end if
  else
     return false
  end if
end checkInternetConnection
Then,

Code: Select all

if checkInternetConnection() then
   -- do your stuff here
end if
Is there a better, faster or more secure way to check internet connection on iPhone?
Please, share your ideas.


Regards,

~ Ender Nafi Elekcioglu
~... together, we're smarter ...~
__________________________________________

macOS Sierra • LiveCode 7 & xCode 8
Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Checking Internet Connection

Post by Dixie »

Hi...

Look at page 53 of the 'iOS Release notes'... and read up on 'Network Reliability Checking'. I've used quite a lot, it says that it is experimental, but it works well.

be well

Dixie
FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: Checking Internet Connection

Post by FireWorx »

What version of Livecode IOS release notes? I checked page 53 and didn't see anything about it. Searched under "Network Reliability Checking" and it returned empty.
Dave
Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Checking Internet Connection

Post by Dixie »

Dave..

LiveCode 5.5.1.... but I did make a mistake, I should have written 'Network reachability' not 'Network reliability'... I did check the release notes again, Page 53 it is...

be well

Dixie
endernafi
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 296
Joined: Wed May 02, 2012 12:23 pm
Contact:

Re: Checking Internet Connection

Post by endernafi »

Thanks Dixie,

I'm reading that section, now.
Actually I'll read whole iOS release notes once again from start.
Obviously, one can miss things out.

Thanks again,

Regards,

~ Ender Nafi Elekcioglu
~... together, we're smarter ...~
__________________________________________

macOS Sierra • LiveCode 7 & xCode 8
FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: Checking Internet Connection

Post by FireWorx »

Hey Dixie,
If you have it working can you supply some sample code. It's a little confusing to me.
Thanks,
Dave
Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Checking Internet Connection

Post by Simon »

Hi Dave,
Before you need your internet connection just put:
iphoneSetReachabilityTarget "http://www.whatever.com" (Well make it meaningful to the site you are reading from).

Then use:
on reachabilityChanged "http://www.whatever.com", tReach
switch tReach
case = "transient"
--do something
break
--and so on
end reachabilityChanged
to monitor that you are still connected.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: Checking Internet Connection

Post by FireWorx »

Cool. This worked for me to let me know if internet was available.
on mouseup
CheckTheNet
end mouseup

in the stack the scripts below were inserted

on CheckTheNet
put "http://www.myserver.on-rev.com" into tMyServer
iphoneSetReachabilityTarget tMyServer, reachabilityChanged
end CheckTheNet

on reachabilityChanged tMyServer, tReach
if "connection required" is among the items of tReach then answer "Internet connection required"
if tReach is "reachable" then answer "The server is accessible"
end reachabilityChanged
FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: Checking Internet Connection

Post by FireWorx »

Well perhaps not so cool ! It stopped working with the following error.

stack "Untitled 1": execution error at line 40 (Handler: can't find handler) near "iPhoneSetReachabilityTarget", char 1

Oh well I guess they say its experimental. Worked fine the other day! go figure
Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Checking Internet Connection

Post by Simon »

hmmmm... that reads like an error from the IDE.
You should have
if the environment is "mobile" then
CheckTheNet

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: Checking Internet Connection

Post by FireWorx »

Ha ha ha.... Yes thanks Simon! I wasn't working in the simulator! I was on my desktop! Silly Me!
Thank You
Post Reply