4

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.

Shawn Frank
  • 4,381
  • 2
  • 19
  • 29
  • Must be something with you not having a data plan... Have you tried on a phone with a data plan and turn airplane mode on and try and connect? What happens then? – Dummy Code Jul 22 '13 at 23:35
  • Right now, if I turn airplane mode on, it will say not connected. That is fine. But I am speaking in terms of someone not having a data which is not uncommon in this part of the world. It would not be good experience when the user does not have a connection, it shows something is loaded only for them to be disappointed 5 mins later when nothing loads. I do not know how 3g and carriers work, perhaps the carrier just restricts the amount of bandwidth a person who does not purchase a data plan is allowed so a connection is detected enough to ping google but not enough to do anything else ? – Shawn Frank Jul 23 '13 at 06:20
  • @ShawnFrank Have you found answer to your question? – Harish Pathak Jan 24 '17 at 06:39

2 Answers2

2

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.

Shawn Frank
  • 4,381
  • 2
  • 19
  • 29
  • After uncountable hours lost pulling my hair, seeing how Apple original Reachability detected "there is internet connection" when my mobile phone was connected to a dead wifi AP, I also opted for this kind of (apparent) brute force solution. You might thing is overkill, but if you create a URL in your own server for it, it's PURE BLISS, and 100% rock solid. – Isaac Dec 13 '17 at 18:21
2

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

enter image description here

Harish Pathak
  • 1,567
  • 1
  • 18
  • 32