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?