View
 

iPhone: Reachability API use

Page history last edited by Shazron Abdullah 6 mos ago
//### EDGE only 

function testReachable() {
          // delay call for 500 ms, in case this is called on document onload event and
          // phonegap is not loaded yet
          setTimeout(function() {
               // no http:// prefix!
               navigator.network.isReachable("www.google.com", testReachable_callback);
               // OR by ipAddress
               //navigator.network.isReachable("212.32.454.32", testReachable_callback, { isIpAddress: true});
          }, 500); 
}
 
// NetworkStatus: 0 for NotReachable, 1 for ReachableViaCarrierDataNetwork,
// 2 for ReachableViaLocalWifiDataNetwork

// NetworkStatus.NOT_REACHABLE = 0;

// NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;

// NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2;

function testReachable_callback(reachability)
{
          alert("hostName: " + reachability.hostName + " " +
                      "ipAddress: " + reachability.ipAddress + " " +
                      "remoteHostStatus: " + reachability. remoteHostStatus + " " +
                      "internetConnectionStatus: " + reachability. internetConnectionStatus + " " +
                      "localWiFiConnectionStatus: " + reachability. localWiFiConnectionStatus
                );
}

Comments (12)

profile picture

Radu said

at 12:34 am on Sep 21, 2009

Is this working on version 0.7.3 or only on EDGE version? I've tried it on 0.7.3 but the callback function is never executed.

profile picture

Adrian Thompson said

at 2:54 am on Jan 26, 2010

I'm assuming this will only work on the device as I cant get it working with the iPhone Simulator (Apple)

profile picture

Shazron Abdullah said

at 3:32 am on Jan 26, 2010

It works fine on the iPhone Simulator, I demoed it the other day.

profile picture

Adrian Thompson said

at 3:58 am on Jan 26, 2010

mmm then I'm doing something wrong. Pasted the above code in the main.js file and calling testReachable() from a button. Using edge 0.8.2 to test. Should that work?

profile picture

Shazron Abdullah said

at 3:59 am on Jan 26, 2010

0.8.2 is not Edge. Edge always means the latest code in the repo. Download the source from http://github.com/phonegap/phonegap-iphone and follow the README on that page.

profile picture

Adrian Thompson said

at 4:02 am on Jan 26, 2010

I followed those instruction but must be doing something wrong. Will it tell me the version in xcode if installed correctly?

profile picture

Adrian Thompson said

at 4:58 am on Jan 26, 2010

Nope can't seem to install edge... followed instructions to a T what am I doing wrong? still tells me I'm using 0.8.2 in the version number within xcode.

profile picture

Adrian Thompson said

at 12:37 pm on Jan 26, 2010

OK I think I have the edge build installed as its now running the reachability api but its still states its 0.8.2 in the version number. Is this a bug or should it give me he correct edge version?

profile picture

Shazron Abdullah said

at 12:43 pm on Jan 26, 2010

Hmm, seems to be an error, the VERSION file was not updated, or was reverted http://github.com/phonegap/phonegap-iphone/blob/master/PhoneGapLib/VERSION . If you got the source for the repo, it should be ok.

profile picture

Adrian Thompson said

at 1:24 pm on Jan 26, 2010

Thanks.. thats why I've been hitting my head against a brick wall all day cos its kept saying 0.8.2 and I kept thinking its not updated to the latest edge.. Thanks for the clarification :)

profile picture

Jordan said

at 12:49 pm on Apr 25, 2010

I almost broke my laptop over this issue....
NOTE: Make sure you do NOT call isReachable within the "deviceready" callback. Call it before or it won't fire.

profile picture

Paul Knox said

at 3:32 am on May 14, 2010

I am completely stuck on trying to implement Reachability so that when my app 1st starts it pops up a message if I don't have a connection. I have tried adding the code above for 3 days. Can anyone please help before I have a breakdown!

Thanks

You don't have permission to comment on this page.