After looking at this post, I've written the following routine to programmatically determine the connectivity status of Cellular and WiFi data.
The routine I've written works well but the information it returns is delayed by approximately 20 to 40 seconds as compared with the status shown by the WiFi and Cellular connectivity icons at the top of the device's screen.
Here's the code:
- (PSTR) isLinkedToTypeCom: (PSTR) reqTypCom andTypeFam: (int) reqTypFam
//...reqTypCom = "pdp_ip0" or "en0" aka (cell or WiFi)
// reqTypFam = AF_INET or AF_INET6 aka ("IPv4" or IPv6")
//
//...derived from from Stack Overflow:
// https://stackoverflow.com/questions/31721696/ios-check-if-cellular-technology-available-even-if-the-device-is-on-wifi
//...returns an ip address string if both the reqTypCom and the reqTypFam are
// matched and the entry is active. otherwise, it will return NULLSTR.
{
struct ifaddrs * pAddrs;
struct ifaddrs * pAddr;
NSString ipAdr = NULLSTR;
if ( getifaddrs( &pAddrs ) == 0 )
{
pAddr = pAddrs;
while( pAddr != NULL )
{
NSString * curTypCom = [NSString stringWithUTF8String: pAddr->ifa_name ];
if ( [curTypCom isEqualToString: reqTypCom] )
{
sa_family_t curTypFam = pAddr->ifa_addr->sa_family;
bool isActive = pAddr->ifa_flags & IFF_UP;
if ( curTypFam == reqTypFam && isActive == true )
{
if ( reqTypFam == AF_INET )
{
ipAdr = [NSString stringWithUTF8String:
inet_ntoa(((struct sockaddr_in *)pAddr->ifa_addr)->sin_addr)];
break;
}
if ( reqTypFam == AF_INET6 )
{
char tmp[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)pAddr->ifa_addr)->sin6_addr), tmp, INET6_ADDRSTRLEN);
ipAdr = [NSString stringWithUTF8String: tmp];
break;;
}
}
}
pAddr = pAddr->ifa_next;
}
freeifaddrs(pAddrs);
}
return( ipAdr );
}
My question: is there something I can do, programmatically, to get the information I want without waiting 20 to 40 seconds?