I am developing an iPhone app and whenever I make a call to my web service I want to make sure that the user is connected to the internet.
I used the Reachability class provided by Tony Million on github, link is her for anyone who wants to grab it. https://github.com/tonymillion/Reachability
The I just followed the examples and set everything up and have the following code
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability*reach)
{
// call web service here
};
reach.unreachableBlock = ^(Reachability*reach)
{
// alert user not reachable
};
[reach startNotifier];
Now I test this in the simulator and everything works perfectly fine, my service is being called when connected to the wifi. And if I switch the wifi off, I see an alert displayed which is exactly what I need.
Now I test this on the device and get decent results but not exactly what I need. It is important to note that my phone does not have a data package.
So here are the scenario's
I know a lot of people have data packages and also I have see apps hit the marketplace with the level of reachability I have mentioned above, I just feel this could be improved in the scenario I just explained.
I have a 3g connection but I do not know how
[Reachability reachabilityWithHostname:@"www.google.com"];
gets pinged, or perhaps its not ?
Although I did switch my wifi off and opened safari, it behaves the same way, it shows that its loading for quite a while and then just says safari could not open ...
Is this just something that cannot be accomplished ?
Finally, I even saw the sample, Tony Million provided on the github page, when run on my phone, it shows reachable despite wifi being off and me not having a data package.
I looked at a few stack over flow answers where users asked about checking for internet connectivity but most answers either revolved on the "type" wifi, wwan etc rather than detecting if it is a valid internet connection rather than being connected to a network.
Thank you for your time.
After quite a few a few days of research I found a solution that was useful for me and hopefully somebody in the future might find it useful.
I created a singleton class with the above reachability code so that I could use it and check it whenever I make web calls easily. Although this did not solve my issue as I mentioned before but this is the first check.
Then I went ahead and just did the regular stuff to get data from the url
NSdata * data [NSData dataWithContentsOfURL:myurlhere];
if([data length] == 0)
{
show alert view here
}
else
{
doing stuff with the items in data
}
So using a combination of reachability (this does not always work in some cases like mine where a cellular network uses 3g or 4g) and checking the data downloaded can serve as a good check point for the app.