IOS Reachability reports no Wi-Fi when WiFi is active

I use Apple's return class to detect network events that affect the functionality of my application. This is a voip application that uses setKeepAliveTimeout, so it wakes up every 10 minutes, reads the network status and decides whether to update the connection.

BOOL res = [app setKeepAliveTimeout:600 handler:^{ [[WIFI instance] isWifiConnected]; [[AClass an_instance] refresh]; } }]; 

So every 10 minutes isWifiConnected is called and the application reads the network status again.

 - (BOOL) isWifiConnected { self.wifiReach = [Reachability reachabilityForLocalWiFi]; NetworkStatus wifiStatus = [self.wifiReach currentReachabilityStatus]; switch (wifiStatus) { case NotReachable: { m_wifiConnected = NO; LOG(@"NetStatus:NotReachable"); break; } case ReachableViaWiFi: { m_wifiConnected = YES; m_wwanConnected = NO; LOG(@"NetStatus:ReachableViaWiFi"); break; } } return m_wifiConnected; } 

Although I have WiFi in the device, the call returns false, i.e. no WiFi, and also NotReachable for network status.

However, after a very short time interval, the reachability callback is called again and the Wi-Fi seems to be connected. However, I already dismissed the event due to an error, and the application closes the connection to the server, believing that there is no wi-fi.

After doing some research, I found this in the Readme file of the Reachability.m file (provided by Apple)

By default, the application uses www.apple.com for its remote host. You can change the host that it uses in APLViewController.m by changing the value of the remoteHostName variable to -viewDidLoad.

IMPORTANT: Reach must use DNS to resolve the host name before it can determine the Reach of this host, and this may take some time on certain network connections. Because of this, the API will return NotReachable before the name resolution is complete. This delay may be visible in the interface on some networks.

.

Could this be a problem? Delay dns search? Or do I need to improve the code?

When I initialize the application, I call it

 self.hostReach = [Reachability reachabilityWithHostName: @"www.apple.com"]; 

If I use an IP address, how is it right?

 self.hostReach = [Reachability reachabilityWithHostName: @"1.2.3.4"]; 

Is it safe to use a public IP address? for example, "17.178.96.59" is the result of nslookup for apple.com

The Reachability class uses a method that appears to be used in an Apple demo.

 - (BOOL)connectionRequired { NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef"); SCNetworkReachabilityFlags flags; if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) { return (flags & kSCNetworkReachabilityFlagsConnectionRequired); } return NO; } 

Why is a connection required? Can be used to solve a problem?

+5
source share
2 answers

Accessibility must be created with the host name, not with an explicit address. The whole point of a DNS system is that sometimes addresses change for hosts. Linking directly to the name server should provide some security in this regard, but that’s not how it should work.

Reachability is often the best guess, not complex and fast. The only way to make sure to actually try. The necessary connection is connected with this, because the device says that "everything looks fine, I just did not try to connect for the real one."

So, perhaps you should run keep keep without checking the reachability status, and use the query result to decide if there is an error or not. If you need to be sure, send the actual request and review the actual result.

0
source

To save battery power, iOS will close network equipment if it is not actively used. This means disabling Wi-Fi and cellular radio stations. In this situation, Reachability will not be able to report the full result, because it cannot verify the situation without returning everything again. That is what kSCNetworkReachabilityFlagsConnectionRequired means - to tell you that you need to make a connection in order to wake up the hardware.

What you see, it is possible that something wakes up when the phone is unlocked (your application or some other application with background permissions), and therefore everything wakes up, you see the error immediately, but then WiFi connects and you are reconnected.

You need to handle Reach as if it could tell you “definitely reachable” or “definitely not reachable”, as well as an “unknown situation”. You need to decide what you are going to do in an unknown situation. If you immediately connect to the network, you will drain the battery faster than usual. An alternative could simply be to wait for the network to wake up for some other reason. It really is up to you.

+1
source

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


All Articles