Question

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 connect my phone to wifi and run the app, perfect, a call is made to the web service. Nothing to worry about here.
  • I disconnect my phone from the wifi and run the app again, I EXPECT the alert to come up telling me that its not connected but then I see the UI activity indicator spinning in my app which means the app has detected a connection of some sort and is trying to connect to my web service. But this will never happen, I know it is detecting the cellular 3g as I go into settings->general->usage->cellular usage, reset the stats to 0. After a while, I can see data being sent and received.
  • I go to settings->general->cellular->turn 3g off, run the app and now it shows the alert of no connectivity.

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.

Was it helpful?

Solution

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.

OTHER TIPS

First, you need to check your cellular data permission for your app in settings.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top