Accessibility iOS speaks of achieving a goal without an Internet connection on 3G

I am developing an application for the iPhone, and whenever I call 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, the link is for everyone who wants to capture it. https://github.com/tonymillion/Reachability

I just followed the examples and set everything up and had 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 am testing this in a simulator, and everything works fine, my service is called when connected to Wi-Fi. And if I turn off Wi-Fi, I see a warning, which is exactly what I need.

Now I test it on the device and get decent results, but not quite what I need. It is important to note that my phone does not have a data packet.

So here is the script

  • I connect my phone to Wi-Fi and run the application perfect, the call is made in the web service. There is nothing to worry about.
  • I will disconnect my phone from Wi-Fi and run the application again. I WAIT for a warning to inform that it is not connected, but then I see a user interface activity indicator spinning in my application, which means that the application has detected some kind of connection and is trying to connect to my web service. But this will never happen, I know that it detects cellular 3G, when I go to settings -> general-> usage-> cellular, reset statistics is 0. After a while I see that the data is being sent and received.
  • I go to settings-> general-> cell-> turn 3g off, start the application, and now it shows a warning about the impossibility of connecting.

I know that many people have data packages, and also I see that the applications hit the market with the reachability level that I mentioned above, I just feel that this can be improved in the scenario that I just explained.

I have a 3g connection, but I don't know how

 [Reachability reachabilityWithHostname:@"www.google.com"]; 

receives ping, or maybe it is not?

Although I turned off Wi-Fi and opened the safari, it behaves the same, it shows that it has been loading for quite a while, and then just says that the safari cannot open ...

Is this something that cannot be achieved?

Finally, I even saw a sample, Tony Million, provided on the github page, when it runs on my phone, it shows that it is available, even though Wi-Fi is disabled and I don't have a data packet.

I looked at several answers on the stream where users asked about checking the Internet connection, but most of the answers either depended on the wifi type, wwan, etc., and did not find out if it is a real Internet connection, and not connected to the network.

Thank you for your time.

+4
source share
2 answers

After several days of research, I found a solution that was useful to me, and I hope someone may find it useful in the future.

I created a singleton class with the reachability code above so that I can use it and test it whenever I make web calls easily. Although this did not solve my problem, as I mentioned earlier, but this is the first check.

Then I went ahead and just did the usual stuff to get the 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 } 

Thus, using a combination of reachability (this does not always work in some cases, such as mine, where the cellular network uses 3g or 4g), and checking the downloaded data can serve as a good reference point for the application.

+2
source

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

enter image description here

+2
source

Source: https://habr.com/ru/post/1492817/


All Articles