Best practice for checking Internet connection status?

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
colourpixels
Posts: 83
Joined: Mon Oct 31, 2011 5:28 am

Best practice for checking Internet connection status?

Post by colourpixels » Thu Dec 22, 2011 6:11 am

Hi All,
I noticed the note on the app store submission tips stating that an app that relies on internet connectivity should have some checks to see if the user is online.

I used the following code alone with a lebel to show the user the status (I'll report back if this app gets approved in the store)

The problem I have is that it seems to return EMPty initially, even when there is a connection (only meant to return that in airplane mode). Also it doesn't seem to fire an went on my iPad when connected to wifi only (no 3g) and I turn off the wifi router. <shrug> they do say it's experimental in the IOS release notes I guess, but if it's mandatory for an app store release it's a bit of a worry.

So my code is something like

Code: Select all

on OpenCard
     iPhoneSetReachabilityTarget  "http://mydomainname.com/"
end OpenCard

on reachabilityChanged pHost,pInfo
 
   if pInfo contains "connection required" then
      set the text of field "labelConnectionOnline" to "Connection required"
   else if pInfo contains "intervention required" then
       set the text of field "labelConnectionOnline" to "Connection required"
   else if pInfo contains "reachable" then
      set the text of field "labelConnectionOnline" to "Connection Ok"
   else if pInfo is empty then
      set the text of field "labelConnectionOnline" to "Are you in airplane mode?"
  end if
What does everyone else do?

Many thanks, look forward to hearing from others as I'm quite new to live code.
:)
Dale

CALL-151
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 206
Joined: Wed Oct 20, 2010 11:00 am

Re: Best practice for checking Internet connection status?

Post by CALL-151 » Thu Dec 22, 2011 6:56 pm

Until it's no longer experimental, I use a simpler approach. Try to open a webpage with iphoneControlSet sBrowserId, "url", "http://www.website.com" and trap errors with something like

Code: Select all

on browserLoadFailed
   answer "Internet connection required"
end browserLoadFailed
This is a crude approach, as the user could still have a viable connection if the site you're trying to load is down. It's best to use a site with a low likelihood of issues like http://www.google.com

This approach has satisfied the Apple reviewers, at least for me.

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

Re: Best practice for checking Internet connection status?

Post by FireWorx » Sat Dec 24, 2011 7:13 am

The code below brings up the google url if my wifi is turned on but does nothing if my wifi is turned off. What am I doing wrong? I am rying to use the example as provided in the docs

global sbrowserID,theUrl

on mouseup
put "http://google.com" into theURL
replace space with "%20" in theURL
iphoneControlSet sbrowserID, "url", theURL
end mouseUp

on browserLoadFailed theURL, pError
answer "Failed to load" && theURL && "Error:" && pError
end browserLoadFailed

Dave

colourpixels
Posts: 83
Joined: Mon Oct 31, 2011 5:28 am

Re: Best practice for checking Internet connection status?

Post by colourpixels » Sat Dec 24, 2011 7:53 am

global gBrowserA

iphoneControlSet gBrowserA["myBrowser"], "url", "http://www.google.com"

works for a mobGUI browser called myBrowser for me

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

Re: Best practice for checking Internet connection status?

Post by FireWorx » Sat Dec 24, 2011 5:04 pm

Hi Colorpixels,
Not sure if I was clear enough in my post. I am not having trouble getting the browser populated when I have internet. I am having trouble returning a message when I don't have internet to alert the user to that and abort the attempt. Have you got that part worked out? If so I would like to see your code.
Thanks,
Dave

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Best practice for checking Internet connection status?

Post by Dixie » Sat Dec 24, 2011 7:32 pm

Dave...

Does this help ?

Code: Select all

on openStack
   if environment() = "mobile" then
      put "http://www.google.com" into theURL
      put URL theURL into connCheck
      
      if connCheck is empty then
         answer "There seems to be no connection to the internet"
         exit openStack
      else
         answer "We are connected to millions of people"
      end if
   end if
end openStack
Ho,ho,ho...

Dixie

colourpixels
Posts: 83
Joined: Mon Oct 31, 2011 5:28 am

Re: Best practice for checking Internet connection status?

Post by colourpixels » Sat Dec 24, 2011 10:42 pm

Sorry Dave, I'll give that code a whirl and see.

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

Re: Best practice for checking Internet connection status?

Post by FireWorx » Sun Dec 25, 2011 1:16 am

HO HO HO !!!
Dixie Nails it !!!!
Worked like a charm !!!
Merry Christmas and Happy New Year !!!!

macnomad2
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 63
Joined: Sat Oct 28, 2006 10:04 pm

Re: Best practice for checking Internet connection status?

Post by macnomad2 » Sun Dec 25, 2011 4:19 pm

Hello Dixie, works fine but not if the user is connected to a portal (for example, first time with a wifi link and then a web page to enter its login and password) because in this case the file sent back is not empty, it is the portal page...
For my app I had to go to a specific file and test the beginning of it against known values : any other answer was a "no connection" situation.

So if you test it against a known page, just add a test that the file you receive starts as it should be. Empty is not enough ;)
Beware that pages like google.com can be forbidden in certain countries or can change without notice.

Jellicle
Posts: 453
Joined: Thu Feb 24, 2011 11:07 am

Re: Best practice for checking Internet connection status?

Post by Jellicle » Thu Dec 29, 2011 4:17 am

I set a global variable to true or false using the reachabilityChanged message, and whenever I need to know if the device is connected I just check that variable. For my purposes checking for connectivity on openstack isn't enough :)

g
14" MacBook Pro
Former LiveCode developer.
Now recovering.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Best practice for checking Internet connection status?

Post by Simon » Sat Jul 07, 2012 1:31 pm

Hi,
I've have tried the solutions posted here but am still getting an error on the actual device (iPad).
The test is:

Code: Select all

on mouseUp
put "" into tConn
put URL "www.mysite.com/connected.html" into tConn
if "This specific string" is in tConn then -- get rid of 404 or other connection alerts
answer "connected"
else
answer "no connection"
end mouseUp
With WIFI on run the app I get "connected"
turn WIFI off I still get "connected". The identical page gets loaded even when offline.
It seems the app is storing the page info and is pulling it from cache.
Does anyone know how to clear the cache or where it is hiding?

THX,
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Best practice for checking Internet connection status?

Post by dave_probertGA6e24 » Sat Jul 07, 2012 6:35 pm

Hi,

Not sure if this will work, but I use a simple cache-breaker method to bypass cached pages on web apps. Simple add a ?x=current time to the end of the URL. e.g...
put URL ("www.mysite.com/connected.html?x=" & seconds) into tConn

This adds the number of seconds since the epoch (eon!) to the end of the request - which means it can never be a cached page.

Look up seconds() in the dictionary to get the correct usage if I have it wrong here :)

I hope that helps.

Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Best practice for checking Internet connection status?

Post by Simon » Sat Jul 07, 2012 7:10 pm

Thanks Dave!
Working now.
put url "www.mysite.com/connected.html?x=" & the seconds into tConn
That works.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Best practice for checking Internet connection status?

Post by dave_probertGA6e24 » Sat Jul 07, 2012 10:06 pm

Glad I could help :)

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

genie
Posts: 108
Joined: Thu Dec 01, 2011 7:19 am

Re: Best practice for checking Internet connection status?

Post by genie » Fri Jan 17, 2014 10:16 am

Used this code from Dixie...

Code: Select all

on openStack
   if environment() = "mobile" then
      put "http://www.google.com" into theURL
      put URL theURL into connCheck
      
      if connCheck is empty then
         answer "There seems to be no connection to the internet"
         exit openStack
      else
         answer "We are connected to millions of people"
      end if
   end if
end openStack
And..upon testing, it works perfectly on iOS, but I get connCheck=EMPTY on Android. Sounds silly, but that's what I get. :shock:
I made sure I am connected to the internet by 'launch'ing theURL afterwards. :|

Any idea why this is happening on my Android?

Thanks Guys!


Regards,
Genie

Post Reply