iOS check if cellular technology available even if the device is on WiFi

前端 未结 2 1791
难免孤独
难免孤独 2021-01-20 05:28

Need some help here.

I need to detect if an iOS device have (on a certain moment) cellular capabilities (No matter which one).

I tried to use reachability cl

相关标签:
2条回答
  • 2021-01-20 06:14

    If anyone needs - this solution I wrote:

    - (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;
    
    }
    
    0 讨论(0)
  • 2021-01-20 06:25

    You can get that info by enumerating network interfaces. Cellular interface is named pdp_ip0. When cellular interface is active and cellular data is enabled it will be up and have an IP address. When you disable cellular data (or don't have cellular connection at all) interface will be down.

    UPDATE

    I will say again, please read my answers carefully. Check IFF_UP, otherwise you're checking non-active interfaces. pdp_ip0 appears twice because one is IPv4 and other is IPv6 - you need to check ifa_addr->sa_family. 255.7.0.0 is a garbage value because that's not a proper way to retrieve an IPv6 address - you need to use inet_ntop. If you do everything correctly then your problem will be solved. Or just try reading documentation - that's all basic well-known APIs that's covered everywhere on the internet.

    Your output exactly matches what I'm seeing on my device:

    • Output on cell disable - here you have IPv6 pdp_ip0 interface but it's down
    • Output on cell enabled - here you see two pdp_ip0 interfaces - first is IPv6, second is IPv4. Both of them will be up
    0 讨论(0)
提交回复
热议问题