IOS checks if cellular technology is available even if the device is connected to WiFi

You need help here.

I need to determine if an iOS device (at some point) has cellular capabilities (no matter which one).

I tried to use the reachability class, but the problem starts when the user connects to wifi, because if that is not possible, the reachability cannot detect cellular

I also tried using this code:

 CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
    NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
    [NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification
                                                    object:nil
                                                     queue:nil
                                                usingBlock:^(NSNotification *note)
    {
        NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
    }];

      

But even if I turn off cellular data, its return CTRadioAccessTechnologyLTE

I cannot figure out why.

Edit

I tried to list the network interface like predastion in the answer below, but pdp_ip0 still works and gets the IP.

struct ifaddrs* interfaces = NULL;
    struct ifaddrs* temp_addr = NULL;

    // retrieve the current interfaces - returns 0 on success
    NSInteger success = getifaddrs(&interfaces);
    if (success == 0)
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL)
        {
            NSString* name = [NSString stringWithUTF8String:temp_addr->ifa_name];



            NSString *address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

            NSLog(@" Name:%@   IP:%@",name,address);


            temp_addr = temp_addr->ifa_next;

        }
    }

    // Free memory
    freeifaddrs(interfaces);

      

Output on disconnect cell:

2015-08-04 11:58:33.297 check[405:46916]  Name:pdp_ip0   IP:255.7.0.0

      

Cell output enabled (pdp_ip0 appears twice)

2015-08-04 11:59:08.914 check[405:46916]  Name:pdp_ip0   IP:255.7.0.0
2015-08-04 11:59:08.914 check[405:46916]  Name:pdp_ip0 P:10.130.112.****)

      

I don't want it to appear twice on it, is there a better way?

Can anyone figure out how I can get this to work? (without using the hidden API).

Many thanks.

+3


source to share


2 answers


You can get this information by listing the network interfaces. The cellular interface is called pdp_ip0

. When the cellular interface is active and cellular data is on, it will be inserted and has an IP address. If you turn off cellular data (or have no cellular connection at all), the interface will be unavailable.

UPDATE

I will say it again, please read my answers carefully. Check IFF_UP

, otherwise you will check inactive interfaces. pdp_ip0

appears twice because one is IPv4 and the other is IPv6 - you need to check ifa_addr->sa_family

. 255.7.0.0

is the value for garbage as this is the wrong way to get an IPv6 address - you need to use inet_ntop

. If you do everything right, your problem will be resolved. Or just try reading the documentation - all the major known APIs that are distributed all over the internet.



Your result matches exactly what I see on my device:

  • Disabled cell exit - here you have an IPv6 interface pdp_ip0

    , but it
  • Cell output is enabled - here you see two interfaces pdp_ip0

    - first IPv6, second - IPv4. Both of them will
+4


source


If someone needs it, I wrote this solution:



- (BOOL)isHaveCell{

    struct ifaddrs* interfaces = NULL;

    struct ifaddrs* temp_addr = NULL;

    // retrieve the current interfaces - returns 0 on success
    NSInteger success = getifaddrs(&interfaces);
    if (success == 0)
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL)
        {

            NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];


            if ([name isEqualToString:@"pdp_ip0"] && temp_addr->ifa_addr->sa_family ==2 ) {

                return TRUE;

            }

            temp_addr = temp_addr->ifa_next;

        }
    }

    // Free memory
    freeifaddrs(interfaces);

    return FALSE;

}

      

+1


source







All Articles