0

Our web service has a "ping" function which is great for testing if the web service is available or not (I don't really care if the internet connection is available, right?) and I know how to test this condition, but how do I go about implementing this in my application? As in where do I test?

If there is no connection, the app doesn't crash of course, it just returns an empty table view or set of views. Should I put the ping before each request and generate an exception or error message when the ping fails? The web service request + response itself can take longer than the transmission of the actual data (latency I guess it would be) so I worry that implementing a Ping before each request might almost double the time it takes to perform each request. Or, should I be continually ping-ing and making sure there is a connection? What is the best practice?

Or should I even worry about it at all?

tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

2 Answers2

1

I was able to use the Reachability library as seen in the answer to How to check for an active Internet connection on iOS or OSX? . You can extend the library to include not only hostReachable but wsReachable with a little bit of work that is probably obvious to all.

Community
  • 1
  • 1
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
0

iOS can provide you with callbacks once the network connectivity changes (e.g. from airplane mode to cell only to wifi and back). So you get notified whenever the network state changes and you don't need to keep pinging the server and can display proper online/offline messages.

Have a look at the SCNetworkReachabilitySetCallback method in the NetworkReachability.h header.

KPK
  • 467
  • 3
  • 9
  • But what about reaching the particular service? Say for instance I have connectivity but the web service just happens to be down. – tacos_tacos_tacos Aug 02 '11 at 23:41
  • I guess then you are on your own... If the ping to verify the actual service takes too long, i would maybe put it into an extra thread that runs at start up and at a low frequency. Then you just need to query a status variable in your main workflow... – KPK Aug 03 '11 at 12:58